check if a string exists in an array that is inside another array

0

I have the following variable with name $array which has the following values

array (size=5)
  0 => 
    array (size=1)
      'name' => string 'a' (length=7)
  1 => 
    array (size=1)
      'name' => string 'b' (length=7)
  2 => 
    array (size=1)
      'name' => string 'c' (length=3)
  3 => 
    array (size=1)
      'name' => string 'd' (length=5)
  4 => 
    array (size=1)
      'name' => string 'f' (length=6)

I have the variable named $check which has the value 'a' .

I want to check if the value of the $check exists in the array of the variable $array

I have tried with in_array , but I can only access by setting the key number in this way:

if(in_array($check, $array[0])):
    echo "existe";
    else:
    echo "nao existe";
endif;
    
asked by anonymous 21.12.2018 / 18:21

1 answer

1

Just scroll through your array and call the function in_array for each value:

function has_value($neddle, $arr)
{
    foreach ($arr as $value) {
        if (in_array($neddle, $value) {
            return true;
        }
    }

    return false;
}

Now, if the number of "array array" levels vary, you will need to do this recursively.

    
21.12.2018 / 18:32