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
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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.