I have a associative array, when i'm looping the array i try to make a statement which output an incorrect answer.
foreach($search as $a=>$b)
{
foreach($b as $c) {
if($b == 'folder')
{
print_r($b);
$dir = true;
}
}
}
The following code outputs 0 and 'folder'. When $b
equals 0 it is incorrect because $b
is supposed to match 'folder'.
$search
array (size=3)
'status' => string 's_search' (length=8)
'files' =>
array (size=1)
'file' =>
array (size=2)
0 =>
array (size=3)
...
1 =>
array (size=3)
...
'folders' =>
array (size=1)
'folder' =>
array (size=3)
'id' => string '1081483096' (length=10)
'name' => string 'asdf' (length=4)
'match_type' =>
array (size=3)
...
Here $b is an array
. So, before loop
you should check whether it is an array or not like,
foreach($search as $a=>$b)
{
if(is_array($b) and !empty($b))
{
foreach($b as $c) {
//your code
}
}
elseif(is_string($b) and $b=='folder')
{
print_r($b);
$dir = true;
}
}
Since you did not specify what you are doing nor what you are expecting as output, I'm assuming that you are only interested in the folders
of your $search
array.
foreach($search['folders'] as $k => $v) {
if($k === 'folder') {
print_r($v);
$dir = true;
}
}
Don't you simply want this?
if (isset($search['folders']['folder'])) {
var_dump($search['folders']['folder']);
$dir = true;
}