8

I know how to check for a value in an array, but how do I check for a value in an Array Iterator?

$array = new ArrayIterator(array(
'1QmRjtsw2UQ' => array('pubdate' => '26 Jun 15', 'alt' => '8 Year Old Beautifully Covers Thinking Out Loud', 'anchor text' => '8-yo \'Thinking Out Loud\''),
'eKqLaYrcf3A' => array('pubdate' => '25 Jun 15', 'alt' => 'Plane Lands On Truck', 'anchor text' => 'Plane Lands On Truck'),
));

I'm trying to check for the values such as 1QmRjtsw2UQ.

This does not work:

if(in_array('1QmRjtsw2UQ', $array));
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
Mike
  • 607
  • 8
  • 30
  • because you've created an arrayiterator, you've actually got an object which has methods and properties. what you need to do is a loop over the elements and make a comparison against the returned values. – Ohgodwhy Jul 06 '15 at 04:42

2 Answers2

6

why don't you use array_key_exists ?

if(array_key_exists('1QmRjtsw2UQ', $array)) 
{
  // do something
}
Akshay Hegde
  • 16,536
  • 2
  • 22
  • 36
  • 1
    This works and makes more sense than using `$array->offsetExists('1QmRjtsw2UQ');` – Mike Jul 06 '15 at 05:45
0

Try this,

$array->offsetExists('1QmRjtsw2UQ');
GAMITG
  • 3,810
  • 7
  • 32
  • 51