2

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 :)

7
  • Why are you using "json_decode" on the elements in the loop? it's already decoded... $anzahl is an object you can iterate in Commented Feb 1, 2017 at 12:46
  • the json is already decoded, your $json variable is an array after row 2 Commented Feb 1, 2017 at 12:47
  • this was the solution yesterday, plz look at the link in the description, or you got a better solution? :) Commented Feb 1, 2017 at 12:47
  • also NEVER reassign the value of a variable within the loop, just remove the first row in the loop Commented Feb 1, 2017 at 12:48
  • wihout the second decoding it throws the error: Notice: Undefined index: Produkt in Commented Feb 1, 2017 at 12:49

3 Answers 3

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Thank You this works for me, also the answer above :)
2

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

Comments

0

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

4 Comments

This isn't the issue, he has 3 json strings in an array. He starts decoding the array, then decodes each inner json in a loop, but reassigned the $json variable which caused the error.
yes i'm a noob in working with json file, but your answer clears a bit for me thank you!
@Antony I would suggest, you read his results.json and that does not deserve a downvote,
@Val he initially decodes the json array, which contains 3 json strings, then loops through all of the values of the decoded array and decodes the inner string. He therefore is decoding the string itself, in the loop

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.