0

I have a matrix¹²³ with the following structure (it's dynamic, may or may not contains those keys (or even more))

array(
    "where" => array(
        "data_col1": "val1",
        "data_col2": "val2"
    ),
    "like" => array(
        "data_col3": "val3"
    )
);

What I need to do is to find if $var_with_data_col_name exists or not.

Using array_key_exists I can check if "where" or "like" exist, but I couldn't find a way to check inside them for a specific key.


PS:

$var_with_data_col_name would be a variable with one of the following strings:

 - data_col1
 - data_col2
 - data_col3
Alireza Fallah
  • 4,609
  • 3
  • 31
  • 57
Michel Ayres
  • 5,891
  • 10
  • 63
  • 97
  • 1
    you should use recursion – Royal Bg Feb 07 '14 at 11:34
  • If you want to check whether that variable value exists or not then you can use "array_key_exists" for that – Snehal S Feb 07 '14 at 11:41
  • I didn't want to use recursion, if there is function that already exist or a simple way. – Michel Ayres Feb 07 '14 at 11:42
  • 2
    @SnehalS but it will simply not work trough the dept of the array. One needs to call a testing function recursively – Royal Bg Feb 07 '14 at 11:42
  • @Michel there's no built in function for that. You need to make your own one. – Royal Bg Feb 07 '14 at 11:42
  • @RoyalBg I'm afraid so. The recursive solution should work for sure (it's kind the "correct" way to do it...) I'm rusty in php, and I couldn't find a function. I wasn't sure if there were any or not. And SO chat is not that great nowadays to those "simple" questions =/ – Michel Ayres Feb 07 '14 at 11:45
  • 1
    @Michel: Unfortunately, given the (lack of) restrictions in the input a recursive function is what you have to use. – Jon Feb 07 '14 at 11:46
  • 1
    @Michel Maybe you should point why you don't want to use this solution? People do write their own utility/helper functions all the way in the project lifetime. Check this question and answer - http://stackoverflow.com/questions/2948948/array-key-exists-is-not-working - the marked answer points how to make a recusirve function out of array_key_exists – Royal Bg Feb 07 '14 at 11:47
  • @RoyalBg it's a great solution, fast and correct. I just wanted to know if there were a better way. I should have used [CodeReview](http://codereview.stackexchange.com/) or [Chat](http://chat.stackoverflow.com/) to do it. – Michel Ayres Feb 07 '14 at 11:52
  • @RoyalBg this question is a dup of the one that you linked. I opened a ticked to close this as dup. Thanks again, sorry for the trouble. – Michel Ayres Feb 07 '14 at 11:53

5 Answers5

1

You can't search for array keys or values in multidimensional arrays directly. Walk through the array and search for it then.

$data_column_1_exists = false;
foreach($array as $key => $value)
{
    if(array_key_exists('data_col1', $value)
        && $key == 'where' //optionally check in specific array
    )
    {
        $data_column_1_exists = true;
    }
}
Ruben
  • 343
  • 2
  • 11
  • Like [@RoyalBg](http://stackoverflow.com/questions/21626674/how-to-find-if-a-key-exist-in-a-matrix/21626711?noredirect=1#comment32679275_21626674) and [@Jon](http://stackoverflow.com/questions/21626674/how-to-find-if-a-key-exist-in-a-matrix/21626711?noredirect=1#comment32679710_21626674) said. Recursive is the way to go :/ – Michel Ayres Feb 07 '14 at 11:50
1

You can use this -

function key_exists_level2($arr, $key){
    foreach($arr as $level1arr){
        if(isset($level1arr[$key])){
            return true;
        }
    }
    return false;
}
//And check with
key_exists_level2($arr, $var_with_data_col_name)
Kamehameha
  • 5,423
  • 1
  • 23
  • 28
0

Itterate through the "main array" and use the same function for checking the keys of each "sub array"

DigiLive
  • 1,093
  • 1
  • 11
  • 28
0

You can use this code, which gives you the key..which has your $var_with_data_col_name .

$data = array(
 "where" => array(
    "data_col1" => "val1",
    "data_col2" => "val2"
 ),
  "like" => array(
    "data_col3" => "val3"
 )
);

$key;
$flag = false;
$data_key = 'data_col1';

foreach($data as $our_key => $array){
if(array_key_exists($data_key,$array)){
    $key = $our_key;
    $flag = true;
}
}

if($flag){
print_r($data[$key]);
}
0

I'm sure there is already a function out there, more of an exercise for myself!

function recursive_array_key_exists($needle, array $haystack) {
    if (array_key_exists($needle, $haystack)) return true;

    foreach($haystack as $value) {
        if (is_array($value)) {
            if (recursive_array_key_exists($needle, $value)) return true;
        }
    }

    return false;
}

Just saw the comment linking to this answer: array_key_exists is not working

I guess mine is basically identical just less code!

Community
  • 1
  • 1
SamV
  • 7,548
  • 4
  • 39
  • 50