Search code examples
phpmultidimensional-arraykeyassociative-arrayarray-multisort

Ordering multidimensional associative arrays


I want to order this array by price AND keep my keys without them being changed.

Here is what I have been working with.

<?php 

$a = array(
        1=>array('price'=>9.25,'timestamp_added'=>1301945848,'name'=>'pencils'),
        4=>array('price'=>19.15,'timestamp_added'=>1299267448,'name'=>'crayon box'),
        15=>array('price'=>4.25,'timestamp_added'=>1299785848,'name'=>'markers'),
        2=> array('price'=>4.28,'timestamp_added'=>1299785848,'name'=>'eraser'),
        44=>array('price'=>13.99,'timestamp_added'=>1299872248,'name'=>'trapper'),
        32=>array('price'=>9.25,'timestamp_added'=>1299872248,'name'=>'notebook'),
        14=>array('price'=>13.99,'timestamp_added'=>1301945848,'name'=>'sharpener'),
        5=>array('price'=>15.01,'timestamp_added'=>1299872248,'name'=>'calculator')
);

function printList( $a ){
    echo "<br />";
    foreach ($a as $key => $value){
        echo "<br /> Product ID $key Price: " . $value['price'] . " Timestamp: " . $value['timestamp_added'] . " Name: " . $value['name'];
    }

}

printList( $a );
$price = array();
foreach ($a as $key => $row)
{
    $price[$key] = $row['price'];
}
array_multisort($price, SORT_ASC, $a);
printList( $a );
?>

Output:

Product ID 1 Price: 9.25 Timestamp: 1301945848 Name: pencils
Product ID 4 Price: 19.15 Timestamp: 1299267448 Name: crayon box
Product ID 15 Price: 4.25 Timestamp: 1299785848 Name: markers
Product ID 2 Price: 4.28 Timestamp: 1299785848 Name: eraser
Product ID 44 Price: 13.99 Timestamp: 1299872248 Name: trapper
Product ID 32 Price: 9.25 Timestamp: 1299872248 Name: notebook
Product ID 14 Price: 13.99 Timestamp: 1301945848 Name: sharpener
Product ID 5 Price: 15.01 Timestamp: 1299872248 Name: calculator

Product ID 0 Price: 4.25 Timestamp: 1299785848 Name: markers
Product ID 1 Price: 4.28 Timestamp: 1299785848 Name: eraser
Product ID 2 Price: 9.25 Timestamp: 1299872248 Name: notebook
Product ID 3 Price: 9.25 Timestamp: 1301945848 Name: pencils
Product ID 4 Price: 13.99 Timestamp: 1299872248 Name: trapper
Product ID 5 Price: 13.99 Timestamp: 1301945848 Name: sharpener
Product ID 6 Price: 15.01 Timestamp: 1299872248 Name: calculator
Product ID 7 Price: 19.15 Timestamp: 1299267448 Name: crayon box

It sorts the array, but the keys are changed. How can I keep the keys labeled the way they were?


Solution

  • You can use uasort function

    $a = array(
               1=>array('price'=>9.25,'timestamp_added'=>1301945848,'name'=>'pencils'),
               4=>array('price'=>19.15,'timestamp_added'=>1299267448,'name'=>'crayon box'),
               15=>array('price'=>4.25,'timestamp_added'=>1299785848,'name'=>'markers'),
               2=> array('price'=>4.28,'timestamp_added'=>1299785848,'name'=>'eraser'),
               44=>array('price'=>13.99,'timestamp_added'=>1299872248,'name'=>'trapper'),
               32=>array('price'=>9.25,'timestamp_added'=>1299872248,'name'=>'notebook'),
               14=>array('price'=>13.99,'timestamp_added'=>1301945848,'name'=>'sharpener'),
               5=>array('price'=>15.01,'timestamp_added'=>1299872248,'name'=>'calculator')
               );
    
    function printList( $a ){
      echo "<br />";
      foreach ($a as $key => $value){
        echo "<br /> Product ID $key Price: " . $value['price'] . " Timestamp: " . $value['timestamp_added'] . " Name: " . $value['name'];
      }
    
    }
    
    printList( $a );
    
    function sbyprice($a, $b)
    {
       return ($a['price'] >= $b['price']) ? 1 : 0;
    }
    
    uasort($a , "sbyprice");
    printList( $a );
    ?>
    

    Output :

    Product ID 1 Price: 9.25 Timestamp: 1301945848 Name: pencils
    Product ID 4 Price: 19.15 Timestamp: 1299267448 Name: crayon box
    Product ID 15 Price: 4.25 Timestamp: 1299785848 Name: markers
    Product ID 2 Price: 4.28 Timestamp: 1299785848 Name: eraser
    Product ID 44 Price: 13.99 Timestamp: 1299872248 Name: trapper
    Product ID 32 Price: 9.25 Timestamp: 1299872248 Name: notebook
    Product ID 14 Price: 13.99 Timestamp: 1301945848 Name: sharpener
    Product ID 5 Price: 15.01 Timestamp: 1299872248 Name: calculator
    
    Product ID 15 Price: 4.25 Timestamp: 1299785848 Name: markers
    Product ID 2 Price: 4.28 Timestamp: 1299785848 Name: eraser
    Product ID 32 Price: 9.25 Timestamp: 1299872248 Name: notebook
    Product ID 1 Price: 9.25 Timestamp: 1301945848 Name: pencils
    Product ID 44 Price: 13.99 Timestamp: 1299872248 Name: trapper
    Product ID 14 Price: 13.99 Timestamp: 1301945848 Name: sharpener
    Product ID 5 Price: 15.01 Timestamp: 1299872248 Name: calculator
    Product ID 4 Price: 19.15 Timestamp: 1299267448 Name: crayon box