php - Remove item from JSON returned from API
Solution:
- Decode the data.
- Remove the item from the array.
- Optionally encode it again as string if needed.
$dataArray = json_decode($data, true);
foreach ($dataArray['Data'] as $key => $item) {
if ($item['Id'] === 11) {
unset($dataArray['Data'][$key]);
}
}
$data = json_encode($dataArray);
Answer
Solution:
it will work
$dataArray = json_decode($data, true);
$dataArray['Data'] = array_filter($dataArray['Data'], function($el){return $el["Id"]<>11;});
$data = json_encode($dataArray);
Answer
Solution:
One way is to re-index by Id
and unset
it:
$array = array_column(json_decode($data, true)['Data'], null, 'Id'));
unset($array[11]);
Now $array
is indexed by Id
. If you want to reset it, then:
$array = array_values($array);
Source