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; } }
Output for 8.0.0 - 8.0.30, 8.1.0 - 8.1.28, 8.2.0 - 8.2.18, 8.3.0 - 8.3.6
array(0) { } bool(true) bool(true) bool(true) bool(true) bool(true) Fatal error: Uncaught Error: Cannot use a scalar value as an array in /in/VqhU3:20 Stack trace: #0 {main} thrown in /in/VqhU3 on line 20
Process exited with code 255.
Output for 7.0.0 - 7.0.33, 7.1.0 - 7.1.33, 7.2.0 - 7.2.33, 7.3.0 - 7.3.33, 7.4.0 - 7.4.33
array(0) { } bool(true) bool(true) bool(true) bool(true) bool(true) Warning: Cannot use a scalar value as an array in /in/VqhU3 on line 20 Fatal error: Uncaught Error: Class 'Log' not found in /in/VqhU3:23 Stack trace: #0 {main} thrown in /in/VqhU3 on line 23
Process exited with code 255.
Output for 5.4.0 - 5.4.45, 5.5.0 - 5.5.38, 5.6.0 - 5.6.40
array(0) { } bool(true) bool(true) bool(true) bool(true) bool(true) Warning: Cannot use a scalar value as an array in /in/VqhU3 on line 20 Fatal error: Class 'Log' not found in /in/VqhU3 on line 23
Process exited with code 255.
Output for 4.4.2 - 4.4.9, 5.1.0 - 5.1.6, 5.2.0 - 5.2.17, 5.3.0 - 5.3.29
Parse error: syntax error, unexpected '[' in /in/VqhU3 on line 14
Process exited with code 255.
Output for 4.3.0 - 4.3.1, 4.3.5 - 4.3.11, 4.4.0 - 4.4.1, 5.0.0 - 5.0.5
Parse error: parse error, unexpected '[' in /in/VqhU3 on line 14
Process exited with code 255.
Output for 4.3.2 - 4.3.4
Parse error: parse error in /in/VqhU3 on line 14
Process exited with code 255.

preferences:
306.72 ms | 401 KiB | 460 Q