简体   繁体   中英

how to sort a multidimensional array in php

I know this question has been asked many times but I have yet to see an array built like this. I have tried some of the numerous examples I have found but none seem to do the trick.

I am editing a PHP program that creates a dropdown list of UPS rates. The functions in this program create an array that is structured like this. I'd like to be able to sort this array on [price].

Array
(

    [price] => Array

        (

            [0] => 617.75

            [1] => 632.97

            [2] => 782.77

            [3] => 597.93

            [4] => 337.00

        )


    [option_value] => Array

        (

            [0] => 07

            [1] => 08

            [2] => 54

            [3] => 65

            [4] => 11

        )


    [option_name] => Array

        (

            [0] => WorldWide Express

            [1] => WorldWide Expedited

            [2] => WorldWide Express Plus

            [3] => International Saver

            [4] => International Standard

        )


)

For ascending, I would try:

array_multisort($array['price'], SORT_ASC, $array['option_value'], $array['option_name']);

To go along with the other answer to restructure the array:

foreach($array['price'] as $key => $value) {
    $result[] = array('price'=>$array['price'][$key],
                      'option_value'=>$array['option_value'][$key],
                      'option_name'=>$array['option_name'][$key]);
}
array_multisort(array_column($result, 'price'), SORT_ASC, $result);

First, I'd refactor the dataset into something sensible:

$i = 0;
    $result = [];
    while($i < count($source['price']) ) {
        $result[$i] = [
                'price' => $source['price'][$i],
                'option_value' => $source['option_value'][$i],
                'option_name' => $source['option_name'][$i]
            ];
        ++$i;
    }

Then, sort the result using usort:

function sort_by_price($a, $b) {
if ($a['price'] == $b['price']) {
    return 0;
}
    return ($a['price'] < $b['price']) ? -1 : 1;
}
usort($result, "sort_by_price");

what will i do is this

$price = $array['price'];
asort($price);
foreach($price as $key=>$val){
    echo $val.'='.$array['option_value'][$key].'='.$array['option_name'][$key].'<br />';
}

i used asort to sort the value ASC and to preserve the array key. you can also use arsort to sort array value DESC.

asort: http://php.net/manual/en/function.asort.php

arsort: http://php.net/manual/en/function.arsort.php

<?php
$ar = array(
       array("10", 11, 100, 100, "a"),
       array(   1,  2, "2",   3,   1)
      );
array_multisort($ar[0], SORT_ASC, SORT_STRING,
                $ar[1], SORT_NUMERIC, SORT_DESC);
var_dump($ar);
?>

array1_sort_order

The order used to sort the previous array argument. Either SORT_ASC to sort ascendingly or SORT_DESC to sort descendingly.

This argument can be swapped with array1_sort_flags or omitted entirely, in which case SORT_ASC is assumed.

Sorting type flags:

SORT_REGULAR - compare items normally (don't change types)
SORT_NUMERIC - compare items numerically
SORT_STRING - compare items as strings
SORT_LOCALE_STRING - compare items as strings, based on the current locale. It uses the locale, which can be changed using setlocale()
SORT_NATURAL - compare items as strings using "natural ordering" like natsort()
SORT_FLAG_CASE - can be combined (bitwise OR) with SORT_STRING or SORT_NATURAL to sort strings case-insensitively

SRC: Example #2 Sorting multi-dimensional array

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM