'Searching for key in multidimensional array in PHP?

I have created a multidimensional array which looks like this:

Array
(
[0] => Array
    (
        [id] => 4
        [questions] => Array
            (
                [title] => This is an example
            )

    )

Now I would like to check if there is a value in this array for key "title". I tried it this way but it still returns false:

$key = array_search($value, array_column($array, 'title'));
// value has the value of the title and $array is the multidimensional array
if ($key !== FALSE){
   echo "Found";
} 
else {
   echo "Not found";
}


Solution 1:[1]

There are many ways to do it. For example :

foreach ($array as $data) {
    if (isset($data['questions']['title'])) {
        echo 'found';
    }
    else {
        echo 'not found';
    }
}

If you want something reusable, maybe you can put a function inside another one ? Like that :

function search($myarray, $mykey) {
    foreach ($myarray as $key => $value) {
        if (is_array($value)) {
            search($value, $mykey);
        }
        else {
            if ($key == $mykey) {
                echo 'found ';
                if ($value != null) {
                    echo 'and value of '.$mykey.' is '.$value;
                }
                else {
                    echo 'and value is null';
                }
            }
        }
    }
};

search($yourarray, 'title');

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1