Nag
Nag

Reputation: 199

Naturally sort a subset of data within a multidimensional array

I have this multidimensional result array in php. I want to sort this array by the values of [name] without using foreach loop.

Array has to sorted by name.

Array
 (
 [result] => Array
    (
        [projects] => Array
            (
                [0] => Array
                    (
                        [name] => Project-3
                        [releases] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 752676125
                                    )
                            )       
                    )
                [1] => Array
                    (
                        [name] => Project-1
                        [releases] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 752676126
                                    )
                             )      
                    )
                [2] => Array
                    (
                        [name] => Project-2
                        [releases] => Array
                            (
                                [0] => Array
                                    (
                                        [id] => 752676127
                                    )
                            )       
                    )
            )
    )
 )

Upvotes: 0

Views: 445

Answers (2)

mickmackusa
mickmackusa

Reputation: 47894

Because the level being sorted consists of data sets with same number of elements and the same parent level keys (name and releases), you can achieve a "REGULAR" (not "NATURAL") sort with plain ol' sort(). Demo

sort($array['result']['projects']);
var_export($array);

For a natural sort with usort(), call strnatcmp(). Demo

usort(
    $array['result']['projects'],
    fn($a, $b) => strnatcmp($a['name'], $b['name'])
);

For a natural sort with array_multisort(), use the SORT_NATURAL constant. Demo

array_multisort(
    array_column($array['result']['projects'], 'name'),
    SORT_ASC, 
    SORT_NATURAL,
    $array['result']['projects']
);

Upvotes: 0

Dushyant Joshi
Dushyant Joshi

Reputation: 3702

First of all extract $mult_arry['result']['projects'] and run the sorting function as follows.

<?php
function msort($array, $key, $sort_flags = SORT_REGULAR) {
    if (is_array($array) && count($array) > 0) {
        if (!empty($key)) {
            $mapping = array();
            foreach ($array as $k => $v) {
                $sort_key = '';
                if (!is_array($key)) {
                    $sort_key = $v[$key];
                } else {
                    // @TODO This should be fixed, now it will be sorted as string
                    foreach ($key as $key_key) {
                        $sort_key .= $v[$key_key];
                    }
                    $sort_flags = SORT_STRING;
                }
                $mapping[$k] = $sort_key;
            }
            asort($mapping, $sort_flags);
            $sorted = array();
            foreach ($mapping as $k => $v) {
                $sorted[] = $array[$k];
            }
            return $sorted;
        }
    }
    return $array;
}


$mult_arry=array('result'=>array('projects'=>array(
        array('name'=>'Project-3','releases'=>array(array('id' => 752676125))),
        array('name'=>'Project-1','releases'=>array(array('id' => 752676126))),
        array('name'=>'Project-2','releases'=>array(array('id' => 752676127)))
)));
$mult_arry_extracted=$mult_arry['result']['projects'];
echo "<pre>";
print_r($mult_arry_extracted);
$mult_arry_sorted_byname = msort($mult_arry_extracted, array('name'));


print_r($mult_arry_sorted_byname);
echo "</pre>";
?>

More information here

Upvotes: 1

Related Questions