Heim  >  Artikel  >  Backend-Entwicklung  >  朋友问小弟我一个PHP的有关问题,自己不会,所以跑来问大家!

朋友问小弟我一个PHP的有关问题,自己不会,所以跑来问大家!

WBOY
WBOYOriginal
2016-06-13 13:33:47801Durchsuche

朋友问我一个PHP的问题,自己不会,所以跑来问大家!!
由于自己是搞JAVA的,但朋友问我一个PHP问题,内容如下:

PHP code
<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)
http://www.CodeHighlighter.com/

-->
$testArr = array(
    'php' => array(
        'author' => 'allen',
        'price' => 40,
    ),
    'java' => array(
        'author' => 'james',
        'price' => 55,
    ),
    'mysql' => array(
        'author' => 'gates',
        'price' => 30,
    ),
    'html' => array(
        'author' => 'bill',
        'price' => 21,
    )
);



请问如何按价格字段进行排序??
借助库函数解决也行!!

------解决方案--------------------
嗯,你是搞JAVA的,价格最贵啊

PHP code

uasort($testArr, create_function('$a,$b', 'return $a["price"]>$b["price"];'));//价格升序,降序改成<font color="#e78608">------解决方案--------------------</font><br>
PHP code


    $testArr = array(
        'php' => array(
            'author' => 'allen',
            'price' => 40,
        ),
        'java' => array(
            'author' => 'james',
            'price' => 55,
        ),
        'mysql' => array(
            'author' => 'gates',
            'price' => 30,
        ),
        'html' => array(
            'author' => 'bill',
            'price' => 21,
        )
    );
    function my_sort($a, $b){
      return $a['price'] > $b['price'];
    }
    uasort($testArr, "my_sort");
    print_r($testArr);
?>
<br><font color="#e78608">------解决方案--------------------</font><br>
PHP code
foreach ($testArr as $v) {
    $k[] = $v['price'];
}
array_multisort($k, SORT_DESC,$testArr);
print_r(array_slice($testArr,0,3));
<br><font color="#e78608">------解决方案--------------------</font><br>楼上几位共使用了两种类型的三种方法<br>对比如下
PHP code
$testArr = array(
    'php' => array(
        'author' => 'allen',
        'price' => 40,
    ),
    'java' => array(
        'author' => 'james',
        'price' => 55,
    ),
    'mysql' => array(
        'author' => 'gates',
        'price' => 30,
    ),
    'html' => array(
        'author' => 'bill',
        'price' => 21,
    )
);

/*** 应用回调函数 ***/
function func1($ar) {
  uasort($ar, create_function('$a,$b', 'return $a["price"]>$b["price"];'));//价格升序,降序改成 $row) {
    $price[$key] = $row['price'];
  }
  array_multisort($price, SORT_ASC,$ar);
}

/*** 应用 php5.3 闭包 ***/
function func3($ar) {
  array_multisort(array_map(function($v){return $v['price'];},$ar),$ar);
}
check_speed(200, 'func2', $testArr);
check_speed(200, 'func3', $testArr);
check_speed(200, 'func1', $testArr); <div class="clear">
                 
              
              
        
            </div>
Stellungnahme:
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn