0

I'm new to PHP working on script to get JSON data from a URL I got the data from the URL and decoded that JSON also.

But issue is that the JSON decoded array is not converting into String. An error comes when I try to run in to foreach loop here is that array example output:

Array name is: $data

OUTPUT:

array(2) {
    [0]=> string(5) "world"
    [1]=> array(12) {
        [0]=> string(15) "worldstarhiphop"
        [1]=> >string(17) "world series 2014"
        [2]=> string(18) "world of solitaire"
        [3]=> string(9) "world map"
        [4]=> string(19) "world's tallest cow"
        [5]=> string(10) "world news"
        [6]=> string(12) "world series"
        [7]=> string(9) "worldstar"
        [8]=> string(12) "world market"
        [9]=> string(29) "worldstarhiphop official site"
        [10]=> string(19) "world's tallest dog"
        [11]=> string(17) "world of warcraft"
    }
}

I tried that:

if(is_array($data)) {
  foreach ($data as $key=>$value) {
    echo $value;
  }
}

Giving this error:

Notice: Array to string conversion in
7
  • 1
    $data is an array containing both strings and arrays. So, in one iteration of the loop, $value is an array. Commented Nov 19, 2014 at 21:45
  • so what i can do to output that array each value? Commented Nov 19, 2014 at 21:47
  • Check if it's an array (is_array), then loop over it and print each element. Commented Nov 19, 2014 at 21:48
  • i already did though check my code Commented Nov 19, 2014 at 21:49
  • 1
    We already know $data is an array. I was talking about $value. Sometimes it's a string, sometimes it's an array. Commented Nov 19, 2014 at 21:51

2 Answers 2

3

This should help you get started. The var_dump is basicly for debugging showing you the contents of the variable.

foreach($data as $foo) {
  if(is_array($foo) {
     foreach($foo as $bar) {      
       var_dump($bar);
     }
  } else {
    var_dump($foo);
  }
}

Consider that this could also be solved in a recursive fashion.

function printNestedArray($foo) 
{
  if(is_array($foo) === false) {
    var_dump($foo);
  } else {
    foreach($foo as $bar) {
      printNestedArray($bar);
    }
  }
}
Sign up to request clarification or add additional context in comments.

4 Comments

i tried this first it made it too much load also it shows "String(No of string in here)" also it return as one string and it's not able to be printed separately too?
Change the var_dump to echo and see what happens.
Hi @AbdullahSiddique, I am not always a big fan of giving the answer out straight, sorry bout that, luckly AbraCadaver has made a pretty concise answer. I am curious, what you mean by "too much load", unless you have a really big array, I would not spend any time on performence.
Thanks both alot :) I'm new to this and still learning the surface :)
1

If your array is always structured like that with 0 as a word and 1 containing an array of related words, then:

echo $data[0];

foreach ($data[1] as $value) {
  echo $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.