Link to home
Start Free TrialLog in
Avatar of is_numeric
is_numeric

asked on

Sort a multid array

Hi

What is the best way to sort a multi-d array ....

Would like to sort the 1st dimension based on  2nd dimension  price3 key value.

Sorted ascending (lowest value first.)

So in my example key [0] would swap with key [1]

[options] => Array
                (
                    [0] => Array
                        (
                            [0] => 519
                            [product_price_id] => 519
                            [1] => 4042
                            [product_id] => 4042
                            [2] => 160
                            [width] => 160
                            [3] => 230
                            [length] => 230
                            [4] => 0
                            [size_id] => 0
                            [5] => 570.00
                            [price1] => 570.00
                            [6] => Was
                            [price1_type] => Was
                            [7] => 285.00
                            [price2] => 285.00
                            [8] => Sale
                            [price2_type] => Sale
                            [9] => 213.75
                            [price3] => 213.75
                            [10] => Now
                            [price3_type] => Now
                            [11] => 
                            [size] => 
                        )

                    [1] => Array
                        (
                            [0] => 518
                            [product_price_id] => 518
                            [1] => 4042
                            [product_id] => 4042
                            [2] => 120
                            [width] => 120
                            [3] => 180
                            [length] => 180
                            [4] => 0
                            [size_id] => 0
                            [5] => 336.00
                            [price1] => 336.00
                            [6] => Was
                            [price1_type] => Was
                            [7] => 168.00
                            [price2] => 168.00
                            [8] => Sale
                            [price2_type] => Sale
                            [9] => 126.00
                            [price3] => 126.00
                            [10] => Now
                            [price3_type] => Now
                            [11] => 
                            [size] => 
                        )

                )

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of Pyromanci
Pyromanci
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
You can use the built-in PHP usort() function for this.  I'll try to give you a code example in a moment.
SOLUTION
Avatar of gr8gonzo
gr8gonzo
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
But let me ask question about this data.  It looks exactly like what someone might get back from a MySQL query when the return was requested via MySQL_Fetch_Array().  In looking at this you will see that you have twice as much data as you need or want.  Each of the columns is duplicated, once with numeric keys and once with named indexes, which are probably the column names.  You really want to fix this.  Use the "Fetch_Object()" function instead.  The syntax is easier to write and the duplicate data will be eliminated, making your application perform faster.

You do not need to sort the data.  You can use the ORDER BY clause in your query.
This will sort it.  But I think you should address the query issue instead.
http://www.laprbass.com/RAY_temp_is_numeric.php

<?php // RAY_temp_is_numeric.php
error_reporting(E_ALL);

// SIMULATED DATA FROM THE POST AT EE
$data = array
( 0 => Array( 'product_price_id' => 519
            , 'price2_type'      => 'Sale'
            , 'price3'           => 213.75
            )
, 1 => Array( 'product_price_id' => 518
            , 'price2_type'      => 'Sale'
            , 'price3'           => 126.00
            )
)
;

// A FUNCTION TO SORT ON 'price3'
function price3_sort($a, $b)
{
    if ($a["price3"]    == $b["price3"]) return 0;
    return ($a["price3"] < $b["price3"]) ? -1 : 1;
}

// COPY ARRAY AND SORT ON NAME
$test = $data;
usort($test, 'price3_sort');

// SHOW THE WORK PRODUCT
print_r($data);
echo PHP_EOL . '<br>';
print_r($test);

Open in new window

Best to all, ~Ray