Reputation: 799
i have this data cennik.json file:
{
"waluta": "PLN",
"vat": 1.23,
"00101": {
"cena": 340,
"powiazanyZ": "00139"
},
"00102": {
"cena": 325.33,
"powiazanyZ": "00140"
},
"00103": {
"cena": 306.67,
"powiazanyZ": "00141"
},
"00104": {
"cena": 289.33,
"powiazanyZ": "00142"
},
"00105": {
"cena": 276,
"powiazanyZ": "00143"
},
"00106": {
"cena": 258.67,
"powiazanyZ": "00144"
},
"00107": {
"cena": 240,
"powiazanyZ": "00145"
},
"00108": {
"cena": 222.67,
"powiazanyZ": "00146"
},
"00109": {
"cena": 205.33,
"powiazanyZ": "00147"
},
"00110": {
"cena": 189.33,
"powiazanyZ": "00148"
},
"00120": {
"cena": 413.33,
"powiazanyZ": "00150"
},
"00121": {
"cena": 73.33,
"powiazanyZ": "00000"
},
"00122": {
"cena": 153.33,
"powiazanyZ": "00000"
},
"00123": {
"cena": 153.33,
"powiazanyZ": "00000"
},
"00138": {
"cena": 1937,
"powiazanyZ": "00152"
},
"00139": {
"cena": 366.82,
"powiazanyZ": "00101"
},
"00140": {
"cena": 325.33,
"powiazanyZ": "00102"
},
"00141": {
"cena": 306.67,
"powiazanyZ": "00103"
},
"00142": {
"cena": 289.33,
"powiazanyZ": "001041"
},
"00143": {
"cena": 276,
"powiazanyZ": "00105"
},
"00144": {
"cena": 258.67,
"powiazanyZ": "00106"
},
"00145": {
"cena": 240,
"powiazanyZ": "00107"
},
"00146": {
"cena": 222.67,
"powiazanyZ": "00108"
},
"00147": {
"cena": 205.33,
"powiazanyZ": "00109"
},
"00148": {
"cena": 189.33,
"powiazanyZ": "00110"
},
"00150": {
"cena": 413.33,
"powiazanyZ": "00120"
},
"00152": {
"cena": 1997.33,
"powiazanyZ": "00138"
}
}
and this from my php file:
$vat = 0.00;
$waluta = '';
$kod00101 = 0.00;
$kod00102 = 0.00;
...
$kod00152 = 0.00;
$cennik_plik = file_get_contents("cennik.json");
$jsonIterator = new RecursiveIteratorIterator(
new RecursiveArrayIterator(json_decode($cennik_plik, TRUE)),
RecursiveIteratorIterator::SELF_FIRST);
foreach ($jsonIterator as $key => $val) {
if(is_array($val)) {
$v = ${'kod'.$key};
if('$key' == $v){
$v = number_format((float)$val[cena],2,'.','');
}
} else {
if('$key' == 'waluta') {$waluta = $val;};
if('$key' == 'vat') {$vat = number_format((float)$val,2,'.','');};
}
}
i am trying this and i get following error:
Fatal error: Uncaught exception 'InvalidArgumentException' with message 'Passed variable is not an array or object, using empty array instead' in C:\Documents and Settings\user\Desktop\Lighty2Go\HTDOCS\ogr\index.php:50 Stack trace: #0 C:\Documents and Settings\user\Desktop\Lighty2Go\HTDOCS\ogr\index.php(50): ArrayIterator->__construct('???{?? "walu...') #1 {main} thrown in C:\Documents and Settings\user\Desktop\Lighty2Go\HTDOCS\ogr\index.php on line 50
Any Suggestions?
I have tried to solve it myself using these resources, but nothing:
Upvotes: 0
Views: 2498
Reputation: 145472
I'm not quite sure what you are attempting with that data. But you certainly don't need that RecursiveArrayIterator
. A boring foreach
is enough to traverse that array:
$json = file_get_contents("cennik.json");
# the culprit was the UTF-8 BOM, which OPs specific verison of json_decode tripped over (newer versions would return NULL)
$json = preg_replace('/^\xEF\xBB\xBF/', '', $json);
$array = json_decode($json, TRUE);
var_dump($array); # <-- WHAT DOES THAT OUTPUT ??
foreach ($array as $key => $value) {
if ($key == "waluta") {
$waluta = $value;
}
elseif ($key == "vat") {
$vat = $value;
}
// all the data entries
elseif (is_numeric($key)) {
$kod[$key] = $value["cena"];
// ${"kod$key"} = ...;
}
}
Note that you can use variable variables. But this example screams array
. I'm not even sure why you would want to use a separate list of variables. The source array should be good enough to work with.
Upvotes: 4