[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="">
)
How can I sort this array alphabetically based on 'term' of the array inside array?
i tried this:
function sortByOrder($a, $b) {
return $search_terms_html[term];
}
uasort($search_terms_html, 'sortByOrder');
but it doesn't work :(
The comparison callback function passed to uasort()
is expected to return a value < 0, 0, or > 0, describing the relationship between its arguments. In your example, the callback is simply returning the the unchanging value $search_terms_html[term]
; you are not using the arguments representing the array elements (and passed as parameters to the callback function, sortByOrder()
). Assuming that the 'term' elements are strings, try defining the callback as:
function sortByOrder($a, $b) {
return strcmp($a['term'],$b['term']);
}
strcmp()
returns values of a sting comparison consistent with the callback's expectations.