-2

I have an array like this:

array(
    [0] => array("description" => "old, white, brown_hair", "name" => "jacques"),
    [1] => array("description" => "young, black, black_hair", "name" => "jack")
);

Now I want to check if the array contains jack. I tried this.

And this is my code.

Community
  • 1
  • 1
  • You have an array of arrays. Simply loop through them and check if each sub-array has a `name` key with the value `jack`, and if so print it and stop the iteration. See [demo](https://eval.in/142739). – Amal Murali Apr 27 '14 at 13:04
  • The recursive function in the other question should have worked as well: `$containsJack = in_array_r('jack', $array);`. See [demo](https://eval.in/142741). – Amal Murali Apr 27 '14 at 13:06
  • https://eval.in/142744 @AmalMurali – I hate Stack Overflow Apr 27 '14 at 13:12
  • @user3578281, Can you just post the `print_r()` or `var_dump()` of your array ? – Shankar Damodaran Apr 27 '14 at 13:16
  • @ShankarDamodaran It is being dynamically generated. – I hate Stack Overflow Apr 27 '14 at 13:19
  • @user3578281: The code you posted wouldn't likely generate the array shown in question. You're not even using the `in_array_r()` function in your code. I'd suggest that you: 1) Verify that array looks likeyou want it to. `var_dump()` the array to make sure it looks like what you think it does. 2) Use `in_array_r()` on the array (as shown in my second comment). – Amal Murali Apr 27 '14 at 13:24

1 Answers1

0

You can use a simple foreach

foreach($arr as $k=>$arr1)
{
 if(in_array('jack',$arr1))
  {
    echo 'jack exists ! exiting....';
    break;
  }
}

Demo

Shankar Damodaran
  • 65,155
  • 42
  • 87
  • 120