chim
chim

Reputation: 51

PHP JSON multiple echo value

In addition to the question yesterday: Question

I got multiple (3) Items in the json file:

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\"}}}"]

I want echo all three value from "Produktkategorie" and the value "MaxBreite"

It should look like this:

Artikel1 - 250
Artikel2 - 250
Artikel3 - 250

My code looks like this:

$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 ++; 
 }

Unfortunately my Code throws a error after passing first line:

Notice: Undefined offset: 1 in

After that I don't get any result.

Kings of coding, could you help me again please :)

Upvotes: 2

Views: 691

Answers (3)

Antony
Antony

Reputation: 1273

The problem is with the $json variable name: You're reassigning it on this line: $json = json_decode($json[$anzahlstart], true);

Rename this variable and you're good to go!

I would also replace the while loop with a foreach loop as shown in my example:

<?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 ++; 
 }

Upvotes: 2

Death-is-the-real-truth
Death-is-the-real-truth

Reputation: 72269

Do you need like this?:-

<?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;
}

Output:-https://eval.in/728430

Note:- If yes then I hope you are able to get other values easily.Thanks

Upvotes: 2

Val
Val

Reputation: 17532

Your problem is, that you are trying to decode an array of json string, instead of the string itself.

so that would be like this now.

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

After reading the other question, I have had this problem before, but you essentially need to do two things. in your ajax.

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

This changes an object into a json string,

Then on your server you do the decode.

something like this

$json = json_decode($jsonstringGoesHERE , true); 

For more information in understanding the issue, have a look at this other post.

jQuery ajax, how to send JSON instead of QueryString

Upvotes: 0

Related Questions