Reputation: 408
This function is intentionally self-referential. I'm looping through an associative array, looking for all instances where a key-value pair matches the desired values. When I look at a value, if it's an array, I look inside.
If a match is found, the array skips forward to the next key-value pair which is where the data I'm interested in is located. I add that data to $array_of_images
successfully.
When it checks every other key-value pair yielding an array, it overwrites $array_of_images
with null rather than ignoring it, as I'd expect.
What am I doing wrong? Please and thank you.
public static function checkArrayItems($item,$key,$val)
{
$array_of_images = [];
foreach ( $item as $k => $v )
{
if ( $k === $key && $v === $val )
{
$details = next($item);
$array_of_images[] = $details;
next($item);
}
else if ( is_array($v) ) self::checkArrayItems($v,$key,$val);
}
return $array_of_images;
}
Upvotes: 0
Views: 57
Reputation: 1180
This is so called recursive function, in your case you are simply not catching the result of recursive calls.
public static function checkArrayItems($item,$key,$val)
{
$array_of_images = [];
foreach ( $item as $k => $v )
{
if ( $k === $key && $v === $val )
{
$details = next($item);
$array_of_images[] = $details;
next($item);
}
else if ( is_array($v) )
$array_of_images = array_merge($array_of_images, self::checkArrayItems($v,$key,$val));
}
return $array_of_images;
}
Upvotes: 1