1

I want to be able to check if a certain key exists within my array. I have an array and I'm merging multiple arrays from mysql. What would be the best way to do so?

for example

Array
(
[0] => Array
(
 [id] => 3
 [comments] => comment text
)
[1] => Array
(
 [id] => 3
 [comments] => comment text
)
[2] => Array
(
 [idMenu] => 1
 [names] => text
)
[3] => Array
(
 [idMenu] => 3
 [names] => names text
)
)

So I'm trying to see if this array has comments and/or names.

do i have to do an if statement?

Thanks

hellomello
  • 8,219
  • 39
  • 151
  • 297
  • possible duplicate of [PHP search for Key in multidimensional array](http://stackoverflow.com/questions/4388967/php-search-for-key-in-multidimensional-array) and couple more (use search function please). – Gordon Dec 31 '11 at 12:21

2 Answers2

5

Assuming that your parent array will always return bunch of children arrays...

foreach ($parentArray as $childArray) {
    if (array_key_exists("comments", $childArray) { return true; }
    if (array_key_exists("names", $childArray) { return true; }
}

Now, that merely checks to see if the parent has a child array with one of those keys.. Actually checking the value to see if it is empty would take a little more code, but this should get you going in the right direction.

abelito
  • 1,094
  • 1
  • 7
  • 18
3

I pulled this function from the manual. It’s a recursive version of array_key_exists(). Since it’s recursive it doesn’t matter how deeply the keys are buried in the array. This function doesn’t tell you where the key may be found — only if it exists.

function array_key_exists_r($needle, $haystack)
{
    $result = array_key_exists($needle, $haystack);
    if ($result) return $result;
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
}

Using your array:

<?php
function array_key_exists_r($needle, $haystack)
{
    $result = array_key_exists($needle, $haystack);
    if ($result) return $result;
    foreach ($haystack as $v) {
        if (is_array($v)) {
            $result = array_key_exists_r($needle, $v);
        }
        if ($result) return $result;
    }
    return $result;
}

$arr = array
(
    array
    (
        'id' => 3,
        'comments' => 'comment text'
    ),
    array
    (
        'id' => 3,
        'comments' => 'comment text'
    ),
    array
    (
        'idMenu' => 1,
        'names' => 'text'
    ),
    array
    (
        'idMenu' => 3,
        'names' => 'names text'
    )
);

var_dump(array_key_exists_r('comments', $arr));
var_dump(array_key_exists_r('names', $arr));
var_dump(array_key_exists_r('bob', $arr));
?>

Output:

bool(true)
bool(true)
bool(false)
Herbert
  • 5,698
  • 2
  • 26
  • 34
  • I'm trying to figure out a way to use a foreach loop to key out the values of different keys when I merge multiple arrays. If I did something like `if(array_key_exists_r('comments',$arr) == 1){//foreach }` then it will echo out all the values that have comments as key but will output errors for the rest of the array. – hellomello Dec 31 '11 at 23:21
  • First, your question was, "how do I check if a certain key exists within my array" so that's what I answered. Second, I don't follow your logic. If you verify that a key exists using `array_key_exists_r()` _before_ echoing its value, then how will it output errors? Am I missing something? – Herbert Jan 01 '12 at 15:05
  • I tried @abelito's method, and I thinkit worked well for what I'm trying to achieve. Thanks for your help too! – hellomello Jan 01 '12 at 19:18