0

I have an array like this:

$array = Array ( [0] => Array ( [id] => 6 ) [1] => Array ( [id] => 6 ) [2] => Array ( [id] => 123 ) [3] => Array ( [id] => 123 ) ) 

In a cycle I use the function

$id = 123;

if (in_array($id, $array)) {
   echo "found!!";
   }
   else
   {               
   echo "not found";
   }

But doesn't works; Why?

Henry8
  • 236
  • 2
  • 6
  • 25
  • You have nested arrays, ``in_array()`` will not check the contained arrays recursively. – user1438038 Aug 28 '15 at 09:54
  • possible duplicate of [in\_array() and multidimensional array](http://stackoverflow.com/questions/4128323/in-array-and-multidimensional-array) – Jonny 5 Aug 28 '15 at 10:40

2 Answers2

6

It will not work because $array contains arrays as values. in_array alone will not help here. Use array_column & in_array together.

in_array($id, array_column($array, 'id'))
Sougata Bose
  • 30,169
  • 8
  • 42
  • 82
-1

Just try

if (array_search($id,$a)) {
   echo "Found";
} else {
   echo "Not Found"
}
rink.attendant.6
  • 36,468
  • 57
  • 89
  • 143
SaviNuclear
  • 882
  • 6
  • 19