0

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
                                    )
                            )       
                    )
            )
    )
 )
8
  • 3
    and the code is ... ? Did you try anything ? Commented Apr 10, 2014 at 5:28
  • 1
    Please show the definition of this array. Commented Apr 10, 2014 at 5:29
  • lol - without foreach loop?? And how do you think php sorting functions work? Foreach or for or while. Commented Apr 10, 2014 at 5:30
  • @Mr.TK Umm we have sorting functions like sort, usort, ksort etc., just saying. Commented Apr 10, 2014 at 5:33
  • @Mr.TK In php array can be sort without foreach loop using sort functions like sort, ksort, u sort, asort, arsort... Commented Apr 10, 2014 at 5:36

2 Answers 2

1

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

Sign up to request clarification or add additional context in comments.

Comments

0

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']
);

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.