我提供的数据只有一行。我有很多不同的“Bestaende”和“Menge”:
[{"Ean":"","Barcode":"010241770148","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90},
{"Ean":"","Barcode":"010241770149","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90},
{"Ean":"","Barcode":"010241770151","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90},
{"Ean":"","Barcode":"010241770152","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90}]
您所描述的foreach
我理解,但它无法按预期工作。
$jsonarray = json_decode($jsonfile,true);
echo 'Array 0 Menge: '.$jsonarray['0']['Bestaende']['0']['Menge'].'<br>';
echo 'Array 1 Menge: '.$jsonarray['1']['Bestaende']['0']['Menge'].'<br>';
echo 'Array 2 Menge: '.$jsonarray['2']['Bestaende']['0']['Menge'].'<br>';
echo 'Array 3 Menge: '.$jsonarray['3']['Bestaende']['0']['Menge'].'<br>';
带出
Array 0 Menge: 1
Array 1 Menge: 1
Array 2 Menge: 1
Array 3 Menge: 1
好。但是当我尝试foreach
时(因为我有一个基础数据列表),输出只有一行。我只得到一个条目“1”。没什么。
$x=0;
foreach($jsonarray[$x]['Bestaende'] as $idx => $Bestaende) {
echo 'Print Menge: ';
print_r($Bestaende['Menge']);
$x++;
}
所以我尝试遍历数组以获取数据。结果是:
Array ( [Filiale] => 1 [FilialeBez] => Laden [Menge] => 1 [Gln] => [Dispo] => 0 )
Print Menge: 1
数组中的数组有问题...是否有更好的方法来获取我想要的数据?
或者我的foreach
循环错了?
答案 0 :(得分:1)
您只能在仅包含一个项目的$jsonarray[0]['Bestaende']
上循环。你应该越过$jsonarray
并从那里深入潜水。
foreach($jsonarray as $item) {
echo 'Print Menge: ';
print_r($item['Bestaende']['0']['Menge']);
// or even dive in deeper
foreach ($item['Bestaende'] as $bestaende) {
// .. $bestaende['Menge'];
}
}
答案 1 :(得分:0)
尝试这样
foreach($jsonarray as $k=>$alldata)
{
foreach($alldata["Bestaende"] as $idx => $Bestaende)
{
echo '<pre>';
echo 'Print Menge: '.$Bestaende['Menge'];
echo '</pre>';
}
}
答案 2 :(得分:0)
Try this code..
$j = '[{"Ean":"","Barcode":"010241770148","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90},
{"Ean":"","Barcode":"010241770149","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90},
{"Ean":"","Barcode":"010241770151","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90},
{"Ean":"","Barcode":"010241770152","Bezeichnung":"Richard 1516","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":269.90,"Vk":269.90}]';
$a = json_decode($j,true);
foreach($a as $key => $value){
if(!empty($value['Bestaende'])){
foreach($value['Bestaende'] as $k=>$v){
echo 'Print Menge: '.(!empty($v['Menge']) ? $v['Menge'] : '').'</br>';
}
}
}