3v4l.org

run code in 300+ PHP versions simultaneously
<?php /* * This code should loop through $data, converting null to 0. * * If it finds an array inside the data, it loops through that * converting null values to 0. * * The error'ed line throws the following PHP error which breaks the code: * ------- * PHP Warning: Cannot use a scalar value as an array * ------- */ $data = json_decode('{"last":"0.692776960","high":"0.692776960","low":"0.692776960","avg":0.6928,"vol":"0.252273837","vols":{"bid":null,"ask":null}}', 1); $new_data = []; foreach($data as $outer_key=>$outer_value) { var_dump($new_data); if(is_array($outer_value)) { $new_key = (string) $outer_key; // ERROR LINE START $new_data[$new_key] = []; // ERROR LINE END foreach($outer_value as $inner_key=>$inner_value) { Log::info("k is " . json_encode($outer_key) . "v is " . json_encode($outer_value) . " p is " . json_encode($inner_key) . " and z is " . json_encode($inner_value) . ""); $new_data[$new_key][$inner_key] = ($inner_value !== null) ?: 0; } } else { $new_data = ($data !== null) ?: 0; } }

preferences:
44.2 ms | 402 KiB | 5 Q