I have the following JSON array, I would like to adjust the array to only keep unique name values, meaning id could be across numerous objects, but name must be unique, overall Tim and Jacob can only be listed once.
$data = '
[
{
"name": "Jacob",
"id": 4
},
{
"name": "Jacob",
"id": 3
},
{
"name": "Tim",
"id": 2
},
{
"name": "Brady",
"id": 2
},
{
"name": "Tim",
"id": 1
}
]';
$array = json_decode($data, TRUE);
$array = array_values( array_unique( $array, SORT_REGULAR ) );
$result = json_encode( $array );
The following is what I have currently tried, but it looks at both values in the object, so it leaves them as is.
Result I am looking for:
[
{
"name": "Jacob",
"id": 4
},
{
"name": "Tim",
"id": 2
},
{
"name": "Brady",
"id": 2
}
]