tags:

views:

404

answers:

5

Tough to explain so here's an example:

$inventory = array(

   array("type"=>"fruit", "price"=>3.50),
   array("type"=>"milk", "price"=>2.90),
   array("type"=>"pork", "price"=>5.43),

);

I would like to sort inventory's element by price so I would like:

$inventory = array(

   array("type"=>"pork", "price"=>5.43),
   array("type"=>"fruit", "price"=>3.50),
   array("type"=>"milk", "price"=>2.90),

);

I'm nearly positive PHP has a function to do this (I'm pretty sure I did it a while ago), but I don't remember!

Thanks all, Matt Mueller

A: 

You might try to define your own comparison function and then use usort.

Alex Sexton
Yes. I'll do that if I can't find a solution. I'm pretty sure there are some weird parameters you can add to one of the sorts to accomplish this. Thanks for your thoughts though!
Matt
A: 

try this: asort($array_to_sort, SORT_NUMERIC);

for reference see this: http://php.net/manual/en/function.asort.php

see various sort flags here: http://www.php.net/manual/en/function.sort.php

gnomixa
+2  A: 
$inventory = 
    array(array("type"=>"fruit", "price"=>3.50),
          array("type"=>"milk", "price"=>2.90),
          array("type"=>"pork", "price"=>5.43),
          );

function pricesort($a, $b) {
  $a = $a['price'];
  $b = $b['price'];
  if ($a == $b)
    return 0;
  return ($a > $b) ? -1 : 1;
}

usort($inventory, "pricesort");
// uksort($inventory, "pricesort");

print("first: ".$inventory[0]['type']."\n\n");
// for usort(): prints milk (item with lowest price)
// for uksort(): prints fruit (item with key 0 in the original $inventory)

// foreach prints the same for usort and uksort.
foreach($inventory as $i){
  print($i['type'].": ".$i['price']."\n");
}

outputs:

first: pork

pork: 5.43
fruit: 3.5
milk: 2.9
danamlund
+2  A: 

Since your array elements are arrays themselves with string keys, your best bet is to define a custom comparison function. It's pretty quick and easy to do. Try this:

function invenDescSort($item1,$item2)
{
    if ($item1['price'] == $item2['price']) return 0;
    return ($item1['price'] < $item2['price']) ? 1 : -1;
}
usort($inventory,'invenDescSort');
print_r($inventory);

Produces the following:

Array
(
    [0] => Array
        (
            [type] => pork
            [price] => 5.43
        )

    [1] => Array
        (
            [type] => fruit
            [price] => 3.5
        )

    [2] => Array
        (
            [type] => milk
            [price] => 2.9
        )

)
zombat
+3  A: 

@Matt: you are right, the function you're looking for is array_multisort()

Here's an example taken straight from the manual and adapted to your case:

$price = array();
foreach ($inventory as $key => $row)
{
    $price[$key] = $row['price'];
}
array_multisort($price, SORT_DESC, $inventory);
Josh Davis
Yesssssss! Thank you that's it.
Matt
Though this is definitely more expensive than the alternatives.
Matt
More expensive? That's weird, on my machine (running PHP 5.3.1-dev) array_multisort() is a few percent faster on small arrays and up to 100 times faster on big arrays (100+ elements)
Josh Davis