Reputation: 29
Original Post: Objects in Arrays...
The data I provided was only one line. I have many lines of the same with different "Bestaende" and "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}]
The foreach
you describe I understand, but it doesn't work as expected .
$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>';
brings out
Array 0 Menge: 1
Array 1 Menge: 1
Array 2 Menge: 1
Array 3 Menge: 1
Okay. But when I try to foreach
it (because I have a list of base data), the output is only one row. I get only one entry "1". Nothing more.
$x=0;
foreach($jsonarray[$x]['Bestaende'] as $idx => $Bestaende) {
echo 'Print Menge: ';
print_r($Bestaende['Menge']);
$x++;
}
So I try to loop over the Array to get the data. The Result is:
Array ( [Filiale] => 1 [FilialeBez] => Laden [Menge] => 1 [Gln] => [Dispo] => 0 )
Print Menge: 1
I have a problem with the array in array in array ... Is there a better way to get the data I want?
Or is my foreach
loop wrong?
Upvotes: 0
Views: 62
Reputation: 177
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>';
}
}
}
Upvotes: 0
Reputation: 2408
Try it like this
foreach($jsonarray as $k=>$alldata)
{
foreach($alldata["Bestaende"] as $idx => $Bestaende)
{
echo '<pre>';
echo 'Print Menge: '.$Bestaende['Menge'];
echo '</pre>';
}
}
Upvotes: 0
Reputation: 15629
You loop only over $jsonarray[0]['Bestaende']
which only contains one item. You should loop over $jsonarray
and from there dive in deeper.
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'];
}
}
Upvotes: 1