[0] => Array (
[term] => punk
[term_html] => <a href=""> punk </a>
)
[1] => Array (
[term] => conflict
[term_html] => <a href=""> conflict </a>
)
[2] => Array (
[term] => Crass
[term_html] => <a href=""> Crass </a>
)
[3] => Array (
[term] => bct 2
[term_html] => <a href="">
)
如何根据&#39; term&#39;按字母顺序对此数组进行排序?数组里面的数组?
我试过这个:
function sortByOrder($a, $b) {
return $search_terms_html[term];
}
uasort($search_terms_html, 'sortByOrder');
但它不起作用:(
答案 0 :(得分:1)
传递给uasort()
的比较回调函数应该返回一个值&lt; 0,0或者&gt; 0,描述其参数之间的关系。在您的示例中,回调只是返回不变的值$search_terms_html[term]
;您没有使用表示数组元素的参数(并作为参数传递给回调函数sortByOrder()
)。假设'term'元素是字符串,尝试将回调定义为:
function sortByOrder($a, $b) {
return strcmp($a['term'],$b['term']);
}
strcmp()
返回与回调预期一致的刺激比较值。
答案 1 :(得分:0)
最简单的方法我找出按其中一个元素对整个多维数组进行排序:
<?php
$multiArray = Array(
Array("id" => 1, "name" => "Defg"),
Array("id" => 2, "name" => "Abcd"),
Array("id" => 3, "name" => "Bcde"),
Array("id" => 4, "name" => "Cdef"));
$tmp = Array();
foreach($multiArray as &$ma)
$tmp[] = &$ma["name"];
array_multisort($tmp, $multiArray);
foreach($multiArray as &$ma)
echo $ma["name"]."<br/>";
?>
输出