我在PHP中有一个关联数组,如:
$newarray1 = Array (
[productA] => Array (
[link] => http://sanjosespartan.com/blog/products/agile-java-development/
[visitcount] => 4
)
[productD] => Array (
[link] => http://sanjosespartan.com/blog/products/intro-to-go/
[visitcount] => 1
)
[productG] => Array (
[link] => http://sanjosespartan.com/blog/products/node-js-for-developers/
[visitcount] => 1
)
[productB] => Array (
[link] => http://sanjosespartan.com/blog/products/beginning-mysql/
[visitcount] => 1
)
[productC] => Array (
[link] => http://sanjosespartan.com/blog/products/computer-networks/
[visitcount] => 1
)
[productF] => Array (
[link] => http://sanjosespartan.com/blog/products/mongo-database/
[visitcount] => 1
)
[productH] => Array (
[link] => http://sanjosespartan.com/blog/products/python/
[visitcount] => 1
)
[productI] => Array (
[link] => http://sanjosespartan.com/blog/string-theory/
[visitcount] => 1
)
[productJ] => Array (
[link] => http://sanjosespartan.com/blog/tcs/
[visitcount] => 1
)
[productE] => Array (
[link] => http://sanjosespartan.com/blog/products/java-complete-refrence/
[visitcount] => 2
)
)
现在我需要根据访问次数对整个数组进行排序。
我尝试使用arsort
,但它没有用。
答案 0 :(得分:0)
您需要一个自定义回调函数,通过查看[" visitcount"]字段来比较两个外部元素。这可以通过 usort 来完成,如果你有一个现代版本的php,你可以使用一个未命名的ad-hoc功能(你不需要一个命名的)。
usort 很简单:给它一个数组和一个函数,告诉它两个参数中哪一个更大。类似的东西:
if($a["visitcount"]>$b["visitcount"]) return 1;
if($a["visitcount"]<$b["visitcount"]) return -1;
return 0;
希望这有帮助。
答案 1 :(得分:0)