jim smith
jim smith

Reputation: 231

How to sort a multidimensional array by a column value?

How do I sort this array by ssup asc?

[
    'xml' => [
        'sale' => [
            [
                'ref' => 316205,
                'line' => 3,
                'ssup' => 'CDA',
                'sdelinfo' => null
            ],
            [
                'ref' => 316657,
                'line' => 1,
                'ssup' => 'Bosch',
                'sdelinfo' => null
            ],
            [
                'ref' => 316791,
                'line' => 1,
                'ssup' => 'Neff',
                'sdelinfo' => null
            ]
        ]
    ]
]

Upvotes: 0

Views: 113

Answers (1)

Sarfraz
Sarfraz

Reputation: 382696

Use usort and supply your own function to do the sorting, e.g.

function cmp($a, $b)
{
    if ($a['ssup'] == $b['ssup']) {
        return 0;
    }
    return ($a['ssup'] > $b['ssup']) ? -1 : 1;
}

usort($array, "cmp");

Upvotes: 5

Related Questions