how can i order this associate array php?

81 views Asked by At

I'm student, i have this array:

 {
        "user": "DAV",
        "checks": [
            {
                "result": "OKEY",
                "idCheck": 13,
            },
            {                             
                "result": "1",
                "idCheck": 14,                  
            }
        ]
    },
    {
        "user": "DAV",            
        "checks": [
            {
                "result": "X",
                "idCheck": 14,
            },
            {
                "idCheck": 13,
                "result": null
            }
        ]
    }

Okey, thats my array, i want to order the "check" by the idCheck, because the second check, first id is 14 and the the seconds is 13. I dont know what i have to do. Maybe usort or something. I don't know

The result i want is this:

{
        "user": "DAV",
        "checks": [
            {
                "result": "OKEY",
                "idCheck": 13,
            },
            {                             
                "result": "1",
                "idCheck": 14,                  
            }
        ]
    },
    {
        "user": "DAV",            
        "checks": [
            {
                "result": null,
                "idCheck": 13,
            },
            {
                "idCheck": 14,
                "result": X
            }
        ]
    }

Thanks.

2

There are 2 answers

0
David Victoria Martinez On BEST ANSWER

The solution is:

 foreach ($resultado as $key => $registro) {

            $registros = json_decode($registro['checks'],true);

            usort($registros, function($a, $b) {

                return $a['idCheck'] <=> $b['idCheck'];

            });

            $resultado[$key]['checks'] = $registros;

        }
0
Mohsen Bagheri On

you can use this function:

arsort(array, sorttype)


arsort($users,SORT_NUMERIC);