3v4l.org

run code in 300+ PHP versions simultaneously
<?php $json = '[{ "provider_id":1, "nro_chart":1, "control": { "color": "blue", "total_value": 21.5, "car_id": 421118 } }, { "control": { "color": "green", "total_value": 25, "car_id": 421119 } }, { "control": { "color": "red", "total_value": 18, "car_id": 421519 } } ]'; $array = json_decode($json,true); // decode as array //list($provider_id,$nro_chart)=array_values(array_splice($array[0],0,2)); // extract and declare single occurrence values $provider_id=$array[0]['provider_id']; $nro_chart=$array[0]['nro_chart']; foreach($array as $index=>&$subarray){ $subarray=['total_value'=>$subarray['control']['total_value'],'car_id'=>$subarray['control']['car_id']]; } echo "provider_id = $provider_id\n"; // display integer value echo "nro_chart = $nro_chart\n"; // display integer value var_export($array); // display updated array
Output for 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.31, 7.4.0 - 7.4.25, 8.0.0 - 8.0.12, 8.1rc1 - rc3
provider_id = 1 nro_chart = 1 array ( 0 => array ( 'total_value' => 21.5, 'car_id' => 421118, ), 1 => array ( 'total_value' => 25, 'car_id' => 421119, ), 2 => array ( 'total_value' => 18, 'car_id' => 421519, ), )

preferences:
40.51 ms | 461 KiB | 5 Q