I am having some problem with foreach statement.Though the input to foreach statement is an array, it says
Invalid argument supplied for foreach()
and my code looks like this
foreach($res_array as $res)
{
foreach($res as $re)
{
echo $re['shortUrl'];
}
}
and my array looks like this
Array ( [errorCode] => 0 [errorMessage] => [results] => Array ( [http://www.telegraph.co.uk/earth/earthpicturegalleries/5966251/The-weirdest-animals-on-Planet-Earth.html?image=5] => Array ( [hash] => 2qNNV6 [shortUrl] => http://su.pr/2qNNV6 ) ) [statusCode] => OK )
I am getting that error for the second foreach. Please help me with this problem.
I think you want to loop over
$res_array['results'], rather than$res_array. You shouldn’t need to nest your foreach loops either.It looks like the result array contains some additional information, so you might want to do something like (untested):