<?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;
}
}
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.
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.
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.