根据大数组内的另一个数组的值对大数组进行排序

     {  "result": true,     
         "users": [{        
                    "id": 11,   
                    "expected_count": 13,
                    "user_id": 1,   
                    "event_id": 2, 
                    "user": {       
                        "id": 1, 
                        "name": "Moiz Jamali"       
                     }  
           },
         {      
            "id": 12,   
            "expected_count": 12,
            "user_id": 2,
            "event_id": 2,      
            "user": {       
                      "id": 2,  
                      "name": "Juzer Samiwala"      
             }  
        }] 
}

Above the an output of my Big Array ('users').

What I want to do is sort this big array ('users') according to the name value which is inside the small array ('user') in PHP.

Can anyone please help me with this?

Thank you.

You can use usort with strcmp for this. Something like this:

$bigArray = ["result" => true, "users" => [
    "id" => 11,
    "expected_count" => 13,
    "user_id" => 1,
    "event_id" => 2,
    "user" => [
        "id" => 1,
        "name" => "Moiz Jamali"
    ],
    ... other users
]];

usort($bigArray['users'], function ($a, $b)
{
    return strcmp($a['user']['name'], $b['user']['name']);
});

You have to user usort() with your own function to sort your array

Solution :

<?php

$data = json_decode('{ "result": true, "users": [{ "id": 11, "expected_count": 13, "user_id": 1, "event_id": 2, "user": { "id": 1, "name": "Moiz Jamali" } }, { "id": 12, "expected_count": 12, "user_id": 2, "event_id": 2, "user": { "id": 2, "name": "Juzer Samiwala" } }] }');


function cmp($a, $b)
{
    if ($a->user->name == $b->user->name) {
        return 0;
    }
    return ($a->user->name < $b->user->name) ? -1 : 1;
}


usort($data->users, "cmp");

print_r($data);

Live example