JSON数组中的对象-无法获取多维数组
我有一个带有这样的字符串的JSON文件:
I have a JSON file with a String like this:
[{"Ean":"","Barcode":"010258100139","Bezeichnung":"Alia","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":129.90,"Vk":129.90}]
我尝试使用PHP将其放入数组,以便获取数据:
with PHP I try to get it into arrays so I can get the data:
$jsonarray = json_decode($jsonfile);
$jsonarray = json_decode(json_encode($jsonarray), true);
var_dump( $jsonarray );
我得到的是以下信息:
array (size=3)
0 =>
array (size=8)
'Ean' => string '' (length=0)
'Barcode' => string '010258100139' (length=12)
'Bezeichnung' => string 'Alia' (length=4)
'Bestaende' =>
array (size=1)
0 =>
array (size=5)
...
'Bestand' => int 1
'Dispo' => int 0
'Uvp' => float 129.9
'Vk' => float 129.9
就这样!我从这里尝试了很多带有嵌套对象的示例. 如果我尝试获取
And thats it! I try many examples from here wit nested objects. If I try to get data like
echo $jsonarray[0]['Barcode'];
我得到的条形码字符串没有问题.但是我无法访问数组(大小= 5)"中的数据. 我尝试了很多事情,例如嵌套的foreach循环,来自这里的函数,例如"object_to_array","json_encode2"等等.
I get my Barcode String without problems. But the data in "array (size=5)" I can't access. I try many things like nested foreach loops, functions from here like "object_to_array", "json_encode2" and so on.
有人可以给我提示如何使用它吗?我尝试获得"Menge":1.
Can anybody give me a tip how I can access it? I Try to get ""Menge":1".
先谢谢了. Tyv
您实际上只需要一个json_decode
,就像这样:
You really only need one json_decode
, like so:
$data = '[{"Ean":"","Barcode":"010258100139","Bezeichnung":"Alia","Bestaende":[{"Filiale":1,"FilialeBez":"Laden","Menge":1,"Gln":null,"Dispo":0}],"Bestand":1,"Dispo":0,"Uvp":129.90,"Vk":129.90}]';
$jsonarray = json_decode($data,true);
打印出来后,您会得到:
When you print it out, you get:
print_r($jsonarray);// I use print_r as its output looks more like how you use it
Array
(
[0] => Array
(
[Ean] =>
[Barcode] => 010258100139
[Bezeichnung] => Alia
[Bestaende] => Array
(
[0] => Array
(
[Filiale] => 1
[FilialeBez] => Laden
[Menge] => 1
[Gln] =>
[Dispo] => 0
)
)
[Bestand] => 1
[Dispo] => 0
[Uvp] => 129.9
[Vk] => 129.9
)
)
因此,从那里,您可以看到如何获取某些数据点.例如,您需要Bestaende的第一个数据数组,因此可以将其抓取为:
So from there, you can see how you can get at certain points of data. For example you wanted Bestaende's first data array, so you would grab it as:
$jsonarray['0']['Bestaende']['0']; // for the whole group
$jsonarray['0']['Bestaende']['0']['Menge']; // for specific items
如果在Bestaende下有多个小组,那么您可以循环进行各自的工作:
If you have multiple groups under Bestaende, then you could loop them doing what you need to do with each:
foreach($jsonarray['0']['Bestaende'] as $idx => $Bestaende) {
$Bestaende['Menge']; // to use that specific data of that group this loop
}