0

I need to sort this multidimensional php object by the variable 'i_lastname'.

I tried most examples with usort and ksort to no avail. Most examples are like this one but won't work for me.

function sort_by_name($a, $b)
{
  return ($a->i_lastname < $b->i_lastname ? -1 : 1);
}
usort($cards, 'sort_by_name');

Here is the object structure.

Thank you.

Array
(
    [0] => Array
        (
            [officer_id] => 4D5DEF60
            [card_id] => 5
            [general] => Array
                (
                    [0] => Array
                        (
                            [i_officer_id] => 4D5DEF60
                            [i_officer_name] => 
                            [i_datetime] => 2012-06-12 13:47:00
                            [i_location] => 17 Rue de Neuch, Yvette, France
                            [i_lat] => 48.6955
                            [i_lon] => 2.21458
                            [i_reason] => DUI
                        )
                )
            [subject] => Array
                (
                    [0] => Array
                        (
                            [subject_id] => 6
                            [i_lastname] => Toro
                            [i_firstname] => Benicio
                        )
                    [1] => Array
                        (
                            [subject_id] => 9
                            [i_lastname] => Limon
                            [i_firstname] => Pedro
                        )
                )
            [vehicle] => Array
                (
                )
        )

    [1] => Array
        (
            [officer_id] => 4D5DEF60
            [card_id] => 2
            [general] => Array
                (
                    [0] => Array
                        (
                            [i_officer_id] => 0f3b45a3
                            [i_officer_name] => 
                            [i_datetime] => 2012-06-06 19:42:00
                            [i_location] => 10231 Yankee St, Miami, USA
                            [i_lat] => 39.5952
                            [i_lon] => -84.2003
                            [i_reason] => Speeding
                        )
                )
            [subject] => Array
                (
                    [0] => Array
                        (
                            [subject_id] => 3
                            [i_lastname] => Doe
                            [i_firstname] => John
                        )
                    [0] => Array
                        (
                            [subject_id] => 15
                            [i_lastname] => Doe
                            [i_firstname] => Jane
                        )
                )
            [vehicle] => Array
                (
                )
        )
)
3
  • Have you tried usort? Haven't tried it myself, but I saw it recommended once :) Commented Jun 12, 2012 at 14:36
  • i can send the codez if you agree to wipe my police record :) Commented Jun 12, 2012 at 14:36
  • So I guess you are trying to sort the subject sub-array - what are you passing to usort()? The entire structure or the sub-array? Commented Jun 12, 2012 at 14:36

1 Answer 1

1

You should use ['...'] rather than ->... - you're dealing with arrays, not objects.

Your callback should look like:

return strnatcasecmp($a['i_lastname'],$b['i_lastname']);

Any of strcmp, strcasecmp, strnatcmp or strnatcasecmp should work fine, you just need to decide which one works best for what you are doing.

Sign up to request clarification or add additional context in comments.

4 Comments

Still does not sort properly. Shouldn't I be specifying that 'i_lastname' is inside the 'subject' array?
Yes. You have to tell it where to find what to search for. So it'd be more like $a['subject'][0]['i_lastname'].
Why the [0] in $a['subject'][0]['i_lastname']?
Because subject is an indexed array with multiple subjects.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.