phparrayssortingmultidimensional-array

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
                                    )
                            )       
                    )
            )
    )
 )

Solution

  • 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