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

PHP

Avatar of undefined
Last Comment
Ray Paseur

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
Pyromanci

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ray Paseur

You can use the built-in PHP usort() function for this.  I'll try to give you a code example in a moment.
SOLUTION
gr8gonzo

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
Ray Paseur

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.
Ray Paseur

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
Experts Exchange is like having an extremely knowledgeable team sitting and waiting for your call. Couldn't do my job half as well as I do without it!
James Murphy
Ray Paseur