我有这个数据cennik.json文件:
{
"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"
}
}
这来自我的php文件:
$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,'.','');};
}
}
我正在尝试这个,我得到以下错误:
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
任何建议?
我试图使用这些资源自己解决它,但没有:
答案 0 :(得分:4)
我不太确定你在尝试使用这些数据。但你当然不需要RecursiveArrayIterator
。枯燥的foreach
足以遍历该数组:
$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"} = ...;
}
}
请注意,您可以使用变量变量。但这个例子尖叫array
。我甚至不确定你为什么要使用单独的变量列表。源数组应该足以使用。