Wednesday, 29 August 2018

How to sort a multidimensional array by first name in php?

I've been pulling my hair out for the past 2 hours now. I'm just trying to sort a multidimensional array by firstName. I've been doing a lot of research and can't really find anything similar to my issue :(. Can anyone help me please? Thanks a lot!!

(
[0] => Array
    (
        [lastName] => Mark
        [firstName] => White
    )

[1] => Array
    (
        [lastName] => Brown
        [firstName] => Peter

[2] => Array
    (
        [lastName] => Paul
        [firstName] => Vo
    )

[3] => Array
    (
        [lastName] => MCLwous
        [firstName] => Luis
    )

[4] => Array
    (
        [lastName] => Zumilia
        [firstName] => Mario
    )

[5] => Array
    (
        [lastName] => Carl
        [firstName] => Burns
    )
)


You can use usort function which sorts arrays using a user-defined comparison function:
usort($array, function($v1, $v2) { return strcmp($v1['firstName'], $v2['firstName']); });

0 comments:

Post a Comment