PHP JSON多重回显值

时间:2017-02-01 12:45:05

标签: php arrays json

除了昨天的问题:Question

我在json文件中有多个(3)项:

results.json

["{\"Produkt\":{\"Produktkategorie\":\"Artikel1\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante1\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel2\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante2\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel3\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante3\"}}}"]

我希望回复来自" Produktkategorie"的所有三个值。和价值" MaxBreite"

它应该是这样的:

Artikel1 - 250
Artikel2 - 250
Artikel3 - 250

我的代码如下所示:

$json = file_get_contents('results.json');
$json = json_decode($json, true);
$anzahl = count($json) -1;
$anzahlstart = 0;

while ($anzahlstart < $anzahl) {
$json = json_decode($json[$anzahlstart], true);                     
$ProduktkategorieFile = $json['Produkt']['Produktkategorie'];
$MaxBreiteFile = $json['Produkt']['Optionen']['MaxBreite'];                     
echo $ProduktkategorieFile. "-" .$MaxBreiteFile;
$anzahlstart ++; 
 }

不幸的是,我的代码在传递第一行后抛出错误:

  

注意:未定义的偏移量:

中的1

之后我没有得到任何结果。

编码之王,请你再帮我一次吧。)

3 个答案:

答案 0 :(得分:2)

你需要这样吗?: -

<?php
$json_string = file_get_contents('results.json');
$json = json_decode($json_string, true); 
// I hope the above line gives you exact json what you shown to us

foreach ($json as $jso){
   $array = json_decode($jso, true);
   echo $array['Produkt']['Produktkategorie'].' - '.$array['Produkt']['Optionen']['MaxBreite'];
   echo PHP_EOL;
}

输出: - https://eval.in/728430

注意: - 如果是,那么我希望您能够轻松获得其他价值。谢谢

答案 1 :(得分:2)

问题在于$json变量名称:您在此行重新分配:$json = json_decode($json[$anzahlstart], true);

重命名这个变量,你就可以了!

我还会用while循环替换foreach循环,如我的示例所示:

<?php
//With foreach
$original = '["{\"Produkt\":{\"Produktkategorie\":\"Artikel1\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante1\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel2\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante2\"}}}","{\"Produkt\":{\"Produktkategorie\":\"Artikel3\",\"Optionen\":{\"MaxBreite\":\"250\",\"MaxHoehe\":\"150\",\"MinBreite\":\"10\",\"MinHoehe\":\"5\",\"ProduktStaerke\":\"3\",\"KantenAuswahl\":\"Kante3\"}}}"]';
$decoded = json_decode($original);

foreach($decoded as $encodedProduct){
    $product = json_decode($encodedProduct,true)['Produkt'];
    echo $product['Produktkategorie'] . " - " . $product['Optionen']['MaxBreite'] . "\n";
}

//Original fixed code
$json = json_decode($original, true);
$anzahl = count($json);
$anzahlstart = 0;

while ($anzahlstart < $anzahl) {
    $decodedJson = json_decode($json[$anzahlstart], true);
    $ProduktkategorieFile = $decodedJson['Produkt']['Produktkategorie'];
    $MaxBreiteFile = $decodedJson['Produkt']['Optionen']['MaxBreite'];                     
    echo $ProduktkategorieFile. " - " .$MaxBreiteFile . "\n";
    $anzahlstart ++; 
 }

答案 2 :(得分:0)

您的问题是,您正在尝试解码array json string,而不是string本身。

所以现在就是这样。

$json = file_get_contents('results.json');
$json = json_decode($json[0], true); // notice [0];on this line.
...

在阅读了另一个问题之后,我之前遇到过这个问题,但你基本上需要做两件事。在你的ajax。

$.ajax({
   ...
   data : JSON.stringify(data)
})

这会将对象更改为json字符串

然后在您的服务器上进行解码。

类似这样的事情

$json = json_decode($jsonstringGoesHERE , true); 

有关理解问题的更多信息,请查看其他帖子。

jQuery ajax, how to send JSON instead of QueryString