Jump to content

Sort this multi-array


therealwesfoster

Recommended Posts

I have an array like so:

 

$pics = array (
array("name" => "wes", "number" => 14),
array("name" => "jared", "number" => 97),
array("name" => "amber", "number" => 2)
);

 

So $pics[0]['name'] will equal wes.

 

Well, I'm wanting to use these in a FOR loop, but I'm wanting to first sort them by their number and have it re-arrange the indexes.

 

So once i sort them, I want it to be in the order 0 = amber,1 = wes,2 = jared instead of the current 0 = wes,1 = jared,2 = amber

 

How would I do this?

 

btw, I've read every sorting method in the php manual.. but for some reason I'm still at a loss.. so please provide an example.

 

Thanks

Link to comment
https://forums.phpfreaks.com/topic/91708-sort-this-multi-array/
Share on other sites

try

<?php
$pics = array (
array("name" => "wes", "number" => 14),
array("name" => "jared", "number" => 97),
array("name" => "amber", "number" => 2)
);
function my_comp($a,$b){
if($a['number']<$b['number']) return -1;
if($a['number']>$b['number']) return 1;
return 0;
}
usort($pics,'my_comp');
print_r($pics);
?>

Link to comment
https://forums.phpfreaks.com/topic/91708-sort-this-multi-array/#findComment-469723
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.