-1

This is the first part of the array (derived from a JSON feed). The repeating blocks are inside [Attendee], and start at [0].

Array
(
[Code] => 1
[Message] => Successfully retrieved data
[Result] => Array
    (
        [Attendee] => Array
            (
                [0] => Array
                    (
                        [CountryCode] => 44
                        [DemographicData] => Array
                            (
                            )

                        [Email] => [email protected]
                        [FreeTextDataCategories] => Array
                            (
                                [AttendeeFreeTextDataCategory] => Array
                                    (
                                        [0] => Array
                                            (
                                                [DataCategoryId] => 165497
                                                [Value] => Firstname
                                            )

                                        [1] => Array
                                            (
                                                [DataCategoryId] => 165498
                                                [Value] => Lastname
                                            )

                                        [2] => Array
                                            (
                                                [DataCategoryId] => 165500
                                                [Value] => Job Title
                                            )

                                        [3] => Array
                                            (
                                                [DataCategoryId] => 165504
                                                [Value] => Array
                                                    (
                                                    )
                                            )
                                    )
                            )

                        [GuestOfAttendeeId] => Array
                            (
                            )

                        [Id] => 3344468
                        [Name] => Firstname Lastname

                        [Password] => Array
                            (
                            )

                        [PhoneNumber] => 123456789
                        [RegisteredVia] => Email
                        [RegistrationDate] => 2017-06-30T11:30:44.313
                        [SessionId] => 111222333
                        [Status] => Registered
                        [StatusDate] => 2017-06-30T11:30:44.313
                    )
            )
    )

I want to sort the Attendee[n] by Lastname, a value I can easily echo, but I can't understand how to sort by it. Here's how I echo it:

foreach ($result_array['Result']['Attendee'] as $row) {    
echo "<a href=\"mailto:" . $row["Email"] . "\">" . $row["FreeTextDataCategories"]["AttendeeFreeTextDataCategory"][0]["Value"] . " " . $row["FreeTextDataCategories"]["AttendeeFreeTextDataCategory"][1]["Value"]. " </a>";  }

I have searched for existing questions and tried to adapt my case to solutions like these, but without success: Sorting Complex Arrays by Name - Sort multidimensional array alphabetically - PHP Array Complex Sort

1 Answer 1

0

Your problem is that Lastname isn't in a field of it's own. So you will have to extract it (and pray that your data is consistent).

function cmp($x, $y) {
    $categoryX = $x['FreeTextDataCategories']['AttendeeFreeTextDataCategory'];
    $lastNameXKey = array_search('165498', array_column($categoryX, 'DataCategoryId'));
    $categoryY = $y['FreeTextDataCategories']['AttendeeFreeTextDataCategory'];
    $lastNameYKey = array_search('165498', array_column($categoryY, 'DataCategoryId'));
    return strcmp($categoryX[$lastNameXKey]['Value'], $categoryY[$lastNameYKey]['Value']);
}

usort($result_array['Result']['Attendee'], 'cmp');
Sign up to request clarification or add additional context in comments.

2 Comments

Sorry if I was unclear, but there are actually three name-related fields in the array. One called ['Name'] (which you used), and two that are a bit "hidden" deeper down in the data, where Firstname and Lastname are separate. They are coupled with [DataCategoryId] 165497 and 165498 respectively. I have tried to refer to ['165498'] in various attempts, but still no luck.
See my edit. In that case you can use array_search and array_column to find the value to compare. ['165498'] won't work because the id isn't a key but a value.

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.