0

I have a JSON file, I am trying to write a script to loop through the json file. I am stuck in the part where value contains multiple images.

[
  {
    "id" : "1",
    "name" : "Michel",
    "value" : "Michel is from US"
  },
  {
    "id" : "2",
    "value" : "Marko is from German",
    "name" : "Marko"
  },
  {
    "id" : "3",
    "name" : "Adam",
    "value" : "Adam is from France"
  },

  {
   "id" : "4",
    "name" : "Photos",
    "value" : [
          {
            "image" : "images/michel.jpg"
          },
          {
            "image" : "images/marko.jpg"
          },
          {
            "image" : "images/adam.jpg"
          }
        ]
    },
{
    "id" : "5",
    "name" : "MARKO",
}
{


      "id" : "6",
        "name" : "EDDI",
        "value" : [


    "001",
      "002"
    ],
    }

Json file looks like written above,

$json = file_get_contents("json/example.json");

// Convert JSON string to Object
$object = json_decode($json);

foreach($object as $key => $value) {
    echo $value->name. ", " . $value->value . "<br>";
}

I got what I was supposed except for values that contain more images, for that part it displays errors.

foreach($object as $key => $value) {
    echo $value->name. ", " . $value->value . "<br>";
}

ERRORS:

Notice: Array to string conversion in C:\xampp\htdocs\test\index.php on line 19

Notice: Undefined property: stdClass::$value in C:\xampp\htdocs\test\index.php on line 19

If a value doesn't exist, I need to print only the "name" andvalue will be empty.

Any advice is appreciated

1
  • Undefined property means that your JSON is malformed or you aren't using it properly. Otherwise based on the example you showed, the value and name properties should alway exist. Commented Aug 14, 2019 at 8:00

2 Answers 2

1

First check if 'value' is array using is_array. After that create inner loop to loop through 'value' contents.

    foreach($object as $key => $value) {
        if(isset($value->value)){
            if(is_array($value->value)){
                foreach($value->value as $key => $val){
                    if(is_object($val)){
                        $stdArray   = (array) $val;
                        $arrayKey   = array_keys($stdArray)[0];
                        echo 'With image value: '. $value->name. ", " . $val->$arrayKey . "<br>";
                    }else{
                        echo 'Single value array: ' . $val . '<br />';
                    }
                }
            }else{
                echo 'Value without image value: '. $value->name. ", " . $value->value . "<br>";
            }
        }else{
            echo 'Only Name If there are no value key exists: ' . $value->name . '<br />';
        }
    }

Notice: Array to string conversion in C:\xampp\htdocs\test\index.php on line 19

This error occurs when you try to echo array. So, always check if the variable you try to echo is array or not by using is_array() function.

Notice: Undefined property: stdClass::$value in C:\xampp\htdocs\test\index.php on line 19

This error occurs when you try to use undefined array key.

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

7 Comments

Hello @Gabriel, it doesnt work. I get Notice: Array to string conversion in C:\xampp\htdocs\test\index.php on line 26
@MuLem check now, i have updated. I did not know you had updated your question.
@ Gabriel great, just one error which I added where in value I have this "value" : [ "001", "002" ]
You have to search array key and use it. that way it will be dynamic, regardless of what key it has it will not fail. It is good for you to learn. Anyway, i have update the answer.
Notice: Trying to get property of non-object in and Catchable fatal error: Object of class stdClass could not be converted to string in. ALl is good except for last parametar in json
|
0

This solution worked for me:

foreach ($object as $wizard) {

    if(is_array($wizard['value'])){

        foreach($wizard['value'] as $key => $val){ 

            $stdArray   = (array) $val;

            foreach($stdArray as $std){

                if(empty($std)){                    
                    $array[] = array($wizard['title'], $val);
                }

                else{ 
                    $array[] = array($wizard['title'], $std);
                }

            } 
        }

    }
    else{ 
        $array[] = array($wizard['title'], $wizard['value']); 
    } 
}

Comments

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.