phparrayssortingmultidimensional-array

Sort a multidimensional array by a column then by first level keys


I've got the following structue of array, but I need sort by mark value and on duplicated need sort at last by array index:

this is my Array base $AsocContData[$s_list_100]:

array(5) {
    [01081] => Array(3){
        [id] => 2
        [Mark] => 420
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00358] => Array(3){
        [id] => 6
        [Mark] => 500
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00277] => Array(3){
        [id] => 3
        [Mark] => 400
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00357] => Array(3){
        [id] => 1
        [Mark] => 500
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
}

What's the best way for order the array by sub-array and on duplicated use the Index array, incremental?

So the results look like this:

array(5) {
    [00277] => Array(3){
        [id] => 3
        [Mark] => 400
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [01081] => Array(3){
        [id] => 2
        [Mark] => 420
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00357] => Array(3){
        [id] => 1
        [Mark] => 500
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
    [00358] => Array(3){
        [id] => 6
        [Mark] => 500
        [lastUpdated] => 2010-03-17 15:44:12
        [dataNext] => Array(2){
            [more1] => 54
            [More2] => 54
        }
    }
}

Update

I try to use this, but it does not have the desired scope with php 7.2:

usort($AsocContData[$s_list_100], function ($a, $b) {
    return $a['Prod_Mark'] <=> $b['Prod_Mark'];
});

this return and not sort for duplicates:

array(20) {
  [0]=>// remplace this first Index array with position and it is a fail.
  array(38) {
    
  }
}

Update

the base array is dynamic nested, and can have much more Array child nested.


Solution

  • You should probably use array_multisort()

    array_multisort(
        array_column( $AsocContData[$s_list_100], 'Mark' ), // sort by the Mark sub-array value first
        SORT_ASC, // ascending
        SORT_NUMERIC, // treat data as numeric
        array_keys( $AsocContData[$s_list_100] ), // secondly, sort by the array indexes
        SORT_ASC, // ascending
        SORT_NUMERIC, // treat indexes as numeric
        $AsocContData[$s_list_100] // this array will be sorted in place and by reference
    );
    
    var_dump( $AsocContData[$s_list_100] );
    

    Input:

    $AsocContData[$s_list_100] = array(
        '01081' => Array(
            'id' => 2,
            'Mark' => 420,
            'lastUpdated' => '2010-03-17 15:44:12'
        ),
        '00358' => Array(
            'id' => 6,
            'Mark' => 500,
            'lastUpdated' => '2010-03-17 15:44:12'
        ),
        '00277' => Array(
            'id' => 3,
            'Mark' => 400,
            'lastUpdated' => '2010-03-17 15:44:12'
        ),
        '00357' => Array(
            'id' => 1,
            'Mark' => 500,
            'lastUpdated' => '2010-03-17 15:44:12'
        )
    );
    

    Output:

    Array
    (
        [00277] => Array
            (
                [id] => 3
                [Mark] => 400
                [lastUpdated] => 2010-03-17 15:44:12
            )
    
        [01081] => Array
            (
                [id] => 2
                [Mark] => 420
                [lastUpdated] => 2010-03-17 15:44:12
            )
    
        [00357] => Array
            (
                [id] => 1
                [Mark] => 500
                [lastUpdated] => 2010-03-17 15:44:12
            )
    
        [00358] => Array
            (
                [id] => 6
                [Mark] => 500
                [lastUpdated] => 2010-03-17 15:44:12
            )
    
    )