Reputation: 1428
I have two JSON encoded arrays
1st Array
{
"User_id":2,
"Seeds":["11","22","31","14"]
}
2nd Array
{
"Seeds": [
{
"Team_name": "Belmont Bruins",
"Team_id": "22",
},
{
"Team_name": "Arkansas State Red Wolves",
"Team_id": "14",
},
{
"Team_name": "Arizona Wildcats",
"Team_id": "11",
},
{
"Team_name": "Brown Bears",
"Team_id": "31",
}
]
}
Now i need to sort the 2nd array based on the 1st array. The 'Seeds' in the first array corresponds to 'Team_id' in the second array. The output required is:
{
"Seeds": [
{
"Team_name": "Arizona Wildcats",
"Team_id": "11",
},
{
"Team_name": "Belmont Bruins",
"Team_id": "22",
},
{
"Team_name": "Brown Bears",
"Team_id": "31",
},
{
"Team_name": "Arkansas State Red Wolves",
"Team_id": "14",
}
]
}
I have found similar questions. But the solutions dosn't seem to work in this case.
Upvotes: 2
Views: 77
Reputation: 158100
You can use the following example. I create a lookup using array_flip()
and sort the second array based on that.
Having this json:
$j1 = <<<EOF
{
"User_id":2,
"Seeds":["11","22","31","14"]
}
EOF;
// I needed to remove addtional commas here
$j2 = <<<EOF
{
"Seeds": [
{
"Team_name": "Belmont Bruins",
"Team_id": "22"
},
{
"Team_name": "Arkansas State Red Wolves",
"Team_id": "14"
},
{
"Team_name": "Arizona Wildcats",
"Team_id": "11"
},
{
"Team_name": "Brown Bears",
"Team_id": "31"
}
]
}
EOF;
... you can do the following:
// Convert to array
$a1 = json_decode($j1, true);
$a2 = json_decode($j2, true);
// Create lookup
$lookup = array_flip($a1["Seeds"]);
// sort array2
usort($a2["Seeds"], function($a, $b) use($lookup) {
if($lookup[$a["Team_id"]] > $lookup[$b["Team_id"]]) {
return 1;
} else if ($lookup[$a["Team_id"]] < $lookup[$b["Team_id"]]) {
return -1;
}
return 0;
});
// Done
var_dump($a2);
Upvotes: 1
Reputation: 477
You can do something like this :)
$a1 = json_decode('{
"User_id":2,
"Seeds":["11","22","31","14"]
}', true);
$a2 = json_decode('{"Seeds": [
{
"Team_name": "Belmont Bruins",
"Team_id": "22"
},
{
"Team_name": "Arkansas State Red Wolves",
"Team_id": "14"
},
{
"Team_name": "Arizona Wildcats",
"Team_id": "11"
},
{
"Team_name": "Brown Bears",
"Team_id": "31"
}]}', true);
$hash = array();
$out = array();
foreach ($a2['Seeds'] as $props) $hash[$props['Team_id']] = $props;
foreach ($a1['Seeds'] as $id) $out[] = $hash[$id];
die(json_encode($out));
Upvotes: 0
Reputation: 26
<?php
$sort_by = [11, 22, 31, 14];
$to_sort = [
array(
"Team_name" => "Belmont Bruins",
"Team_id"=> 22,
),
array(
"Team_name" => "Arkansas State Red Wolves",
"Team_id" => 14,
),
array(
"Team_name" => "Arizona Wildcats",
"Team_id" => 11,
),
array(
"Team_name" => "Brown Bears",
"Team_id" => 31,
)
];
$hash = [];
foreach ($to_sort as $value){
$hash[$value["Team_id"]] = $value;
}
$len = count($sort_by);
$sorted = [];
for ($i=0; $i<$len; $i++)
$sorted[] = $hash[$sort_by[$i]];
var_dump($sorted);
?>
Upvotes: 1
Reputation: 417
You should decode json format, sort array and then encode to json format:
$array1 = json_decode($array1_json);
$array2 = json_decode($array2_json);
foreach ($array1['Seeds'] as $order)
{
foreach ($array2['Seeds'] as $data)
{
if ($data['Team_id'] == $order)
$array_sorted[] = $data;
}
}
$array2_sorted = array('Seeds' => $array_sorted);
echo json_encode($array2_sorted);
Upvotes: 1