my array have this structure:
$news = array('id' => '', 'views' => '');
i need to order this by the most viewed, but it have to match the id. I've done some research but got many doubts.
my array have this structure:
$news = array('id' => '', 'views' => '');
i need to order this by the most viewed, but it have to match the id. I've done some research but got many doubts.
I'm having some trouble deciphering exactly what you mean with the question but here a couple of thinks that may work for you.
if your array is composed like
array(
array("id"=>1, "views"=>50),
array("id"=>3, "views"=>16)
);
Then you can use usort with a closure(php 5.3)
usort($news, function ($a, $b){ return $a["views"] - $b["views"]});
if you want it in descending order you can just swap the $a and $b in the closure
usort($news, function ($a, $b){ return $b["views"] - $a["views"]});
If you have an array that is composed like
array(
"id"=>array(1,2),
"views"=>array(50, 16)
);
Then you can use array_multisort
array_multisort($news["views"], $news["id"]);
or if you want it in descending order
array_multisort($news["views"], SORT_DESC, SORT_NUMERIC, $news["id"]);
UPDATE
The final solution.. news was an object with a views and id array properties. The final solution was:
array_multisort($news->views, $news->id);