0

Here is my code.

function displayTweets($object){
    $myArray = json_decode(json_encode($object), true);
    foreach ($myArray["statuses"] as $tweet){

It gives me a warning when I use line 3 of my code. The $object passed in the parameter is a json object and I am trying to for loop through it and display tweets. The code at first doesn't produce warnings but then out of no where it will and it pops my page with a warning box. How can I stop this warning message? Is it okay to use $myArray["statuses"] ?

1
  • KeyMaker13 check $myArray contains status index or not. You can use isset(), !empty(), array_key_exists() Commented Mar 5, 2021 at 4:57

2 Answers 2

2

You can try this

function displayTweets($object){
   $myArray = json_decode(json_encode($object), true);
   if(array_key_exists("statuses", $myArray) {
      foreach ($myArray["statuses"] as $tweet){
      // Your code
      }
    }
 }
Sign up to request clarification or add additional context in comments.

Comments

1

Check to see if $myArray actually contains the statuses key before using it.

if(isset($myArray['statuses'])) {
  // $myArray contains statuses, execute the for loop
}

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.