由于自己是搞JAVA的,但朋友问我一个PHP问题,内容如下:$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,
)
);
请问如何按价格字段进行排序??
借助库函数解决也行!!
'php' => array(
'author' => 'allen',
'price' => 40,
),
'java' => array(
'author' => 'james',
'price' => 55,
),
'mysql' => array(
'author' => 'gates',
'price' => 30,
),
'html' => array(
'author' => 'bill',
'price' => 21,
)
);
请问如何按价格字段进行排序??
借助库函数解决也行!!
uasort($testArr, create_function('$a,$b', 'return $a["price"]>$b["price"];'));//价格升序,降序改成<
<?
$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);
?>
$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,));foreach ($testArr as $key => $row) {
$names[$key] = $row['author'];
$price[$key] = $row['price'];
}array_multisort($price, SORT_ASC,$testArr);
print_R($testArr);
$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,));
array_multisort(array_map(function($v){return $v['price'];},$testArr),$testArr);
print_r($testArr);也来凑个热闹~
<?
$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");
$testArr = array_slice($testArr, 0, 3);
print_r($testArr);
?>
$k[] = $v['price'];
}
array_multisort($k, SORT_DESC,$testArr);
print_r(array_slice($testArr,0,3));
可以用array_filter 或者 array_map过滤出前3名....代码? ..... 嗯, 我今日小懒, 楼下那谁, 麻烦写下, 谢谢
对比如下$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"];'));//价格升序,降序改成<
}/*** 不使用回调函数 ***/
function func2($ar) {
foreach ($ar as $key => $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);200次运行的平均耗时func2
时间: 5 微秒
内存: 0func3
时间: 6 微秒
内存: 88func1
时间: 20 微秒
内存: 214656
'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 func($ar) {
$r = array(0);
foreach($ar as $k=>$v) {
if($v['price'] > min($r)) $r[$k] = $v['price'];
if(count($r) > 3) unset($r[array_search(min($r), $r)]);
}
}check_speed(200, 'func', $ar); func
时间: 6 微秒
内存: -8
$_testArr[$val['price']] = $val;
ksort($_testArr);
print_r($_testArr);怎么不这么写,不是很快么。
这样写好像很土,php array函数好多,我常用就那么几个= =
foreach ($testArr as $key=>$val)
{
$val['key'] = $key;
$_testArr[$val['price'].$key] = $val;
}
ksort($_testArr);$testArr = array();
foreach ($_testArr as $key=>$val)
{
unset($val['key']);
$testArr[$_testArr[$key]['key']] = $val;
}
print_r($testArr);
$_testArr[$val['price']] = $val;
ksort($_testArr);
print_r($_testArr);
Array
(
[21] => Array
(
[author] => bill
[price] => 21
) [30] => Array
(
[author] => gates
[price] => 30
) [40] => Array
(
[author] => allen
[price] => 40
) [55] => Array
(
[author] => james
[price] => 55
))这是你期望得到的结果吗
http://php.net/manual/en/function.create-function.php
手册这里的回复里有好几条是关于这个泄漏问题的.bugs这里有问题及相关回复:
https://bugs.php.net/bug.php?id=6333实际就是create_function每次都建一个隐藏的函数,但是不会(无法)去注销过去建的同样函数,
于是每次都增加点内存使用一般的网页里不会有太大问题,因为运行很快结束并释放所有内存.但是在长时间运行的脚本里会有问题,
我第一次注意到这个就是在一个抓网页的脚本里,因为要抓很多页面,会运行很长时间,
php5.3后用新的匿名函数就没问题了 (因为那个实际是一个closure类,不会重复建函数)