1

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

Array has to sory by [name]. Thanks in advance.

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
                                    )
                            )       
                    )
            )
    )
 )
Nag
  • 181
  • 3
  • 13
  • 2
    and the code is ... ? Did you try anything ? – CMPS Apr 10 '14 at 05:28
  • 1
    Please show the definition of this array. – Dave Chen Apr 10 '14 at 05:29
  • lol - without foreach loop?? And how do you think php sorting functions work? Foreach or for or while. – Mr.TK Apr 10 '14 at 05:30
  • @Mr.TK Umm we have sorting functions like _[sort](http://www.php.net/manual/en/function.sort.php)_, _[usort](http://www.php.net/manual/en/function.usort.php)_, _[ksort](http://www.php.net/manual/en/function.ksort.php)_ etc., just saying. – AyB Apr 10 '14 at 05:33
  • @Mr.TK In php array can be sort without foreach loop using sort functions like sort, ksort, u sort, asort, arsort... – Nag Apr 10 '14 at 05:36
  • possible duplicate of [Sort Multi-dimensional Array by Value](http://stackoverflow.com/questions/2699086/sort-multi-dimensional-array-by-value) – Mr.TK Apr 10 '14 at 05:38
  • Tried that, but didn't work and getting this Fatal error: Cannot use string offset as an array – Nag Apr 10 '14 at 05:40
  • @Nag You have to edit the code a bit in the link provided by TK, _[like in here](http://codepad.org/Mip1kS8X)_. – AyB Apr 10 '14 at 05:46

1 Answers1

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

Dushyant Joshi
  • 3,672
  • 3
  • 28
  • 52