How to sort array by something in php? -


this question has answer here:

i have data this

array([0] => stdclass object ( [id] => 1 [title] => aaaa [sentence] => abcdefgh [rank] => 3 ) [1] => stdclass object ( [id] => 2 [title] => bbbb [sentence] => ijklmn [rank] => 1 ) [3] => stdclass object ( [id] => 3 [title] => ccc [sentence] => opqrstu [rank] => 2 ));

show data:

foreach($data $d) {    $title = $d->title;    $sentence = $d->sentence;    $rank = $d->rank;    echo $title. " | " .$sentence. " | " .$rank. "<br>"; } 

how sort 'rank'? help.

you can use php's usort function uses objects' attribute rank criteria:

function rank_compare($a, $b) {     return $a->rank - $b->rank; }  usort($data, 'rank_compare'); 

more info: http://www.php.net/manual/en/function.usort.php


Comments

Popular posts from this blog

linux - xterm copying to CLIPBOARD using copy-selection causes automatic updating of CLIPBOARD upon mouse selection -

c++ - qgraphicsview horizontal scrolling always has a vertical delta -