3v4l.org

run code in 150+ php & hhvm versions
Bugs & Features
<?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 7.0.0 - 7.1.0
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.6.28
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.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.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.