1

I have a multidimensional array that looks like this:

<?php
$array = array(
"categories" => array(
    array(
        array(
            "arr1" => array(
                'Name' => "some name associated with 300",
                'availability' => true,
                'amount' => 300
            ),
            "arr2" => array(
                'Name' => "some other name",
            ),
            "arr3" => array(
                'Name' => "some other name",
            )
        )
    ),
    array(
        array(
            "arr1" => array(
                'Name' => "some name associated with 59",
                'availability' => true,
                'amount' => 59
            ),
            "arr2" => array(
                'Name' => "some other name",
            ),
            "arr3" => array(
                'Name' => "some other name",
            )
        )
    ),
    array(
        array(
            "arr1" => array(
                'Name' => "some name associated with 100",
                'availability' => true,
                'amount' => 100
            ),
            "arr2" => array(
                'Name' => "some other name",
            ),
            "arr3" => array(
                'Name' => "some other name",
            )
        )
    )
),
"departures" => array(
    //..same as above arrays..
),
"arrivals" => array(
    // ..same as above arrays..
),
//..more arrays
);
?>

I am trying to sort it by the amount key value (From Smallest to Largest) so that I can have a result like this:

$array = array(
    "categories" => array(
        array(
            array(
                "arr1" => array(
                    'Name' => "some name associated with 59",
                    'availability' => true,
                    'amount' => 59
                ),
                "arr2" => array(
                    'Name' => "some other name",
                ),
                "arr3" => array(
                    'Name' => "some other name",
                )
            )
        ),
        array(
            array(
                "arr1" => array(
                    'Name' => "some name associated with 100",
                    'availability' => true,
                    'amount' => 100
                ),
                "arr2" => array(
                    'Name' => "some other name",
                ),
                "arr3" => array(
                    'Name' => "some other name",
                )
            )
        ),
        array(
            array(
                "arr1" => array(
                    'Name' => "some name associated with 300",
                    'availability' => true,
                    'amount' => 300
                ),
                "arr2" => array(
                    'Name' => "some other name",
                ),
                "arr3" => array(
                    'Name' => "some other name",
                )
            )
        )
    ),
    "departures" => array(
    //..same as above arrays..
    ),
    "arrivals" => array(
    //..same as above arrays..
    ),
);

I have tried to iterate through the arrays and create a new one but I am repeatedly getting stuck.. unless there's a completely different way to do this. Can you please help me out?.. This is what I have tried so far:

$ordered_arr = array(
    "departures" => array(),
    "arrivals" => array(),
    "categories" => array()
);
foreach ($array as $key => $arrOpts) {
    $new_key = "";
    foreach ($arrOpts as $arrVals) {    
        if (empty($ordered_arr[$key])) {
            array_push($ordered_arr[$key], $arrVals);
        } else {
            var_dump();
            /// this is where im stuck
        }
    }
}
CodeGodie
  • 12,116
  • 6
  • 37
  • 66
  • You can take a look for this post: http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value Solutions are very similiar. – Szymon Dudziak Nov 19 '14 at 21:32
  • Can you please explain how `usort` can be used in this occassion? It seems that it works for a one level multidimensional array, but how do we use it if the array has more than one level as the example above? – CodeGodie Nov 19 '14 at 21:37

1 Answers1

1
$categories = array();
foreach ($array['categories'] as $values) {
    foreach ($values as $value) {
        $categories[] = $value;
    }
}
$amounts = array();
foreach ($categories as $category) {
    foreach ($category as $v) {
        if (isset($v['amount']) === true) {
            $amounts[] = $v['amount'];
        }
    }
}
array_multisort($amounts, SORT_ASC, $categories);
foreach ($categories as &$category) {
    $category = array($category);
}
unset($category);
$array['categories'] = $categories;
var_dump($array);
cetver
  • 11,279
  • 5
  • 36
  • 56