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
        }
    }
}
2
  • You can take a look for this post: stackoverflow.com/questions/2699086/… Solutions are very similiar. Commented Nov 19, 2014 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? Commented Nov 19, 2014 at 21:37

1 Answer 1

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);
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.