Jump to content

in_array not working as expected

- - - - -

  • Please log in to reply
2 replies to this topic

#1
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
I was under the impression that in_array can search with an array value.
I noticed that it does not work for the following code:
$array1 = array(1, 2);

$array2 = array(1, 2, 3, 4, 5);


echo in_array($array1, $array2) ? 'Found' : 'Lost';

Is this expected behavior? If so can you help me better understand why? (perhaps I am just tired from working all day and it's obvious)
I am trying to identify if an array contains any of the values from the given search array.

I read the in_array manual an it shows an example with a multidimensional array but not a simple array.

Would it just be more efficient to loop over the search array and manually check the values? (This is what I am about to resort to)

Thanks for any help on the matter or just fixing my stupid.
"Life would be so much easier if we only had the source code."

#2
WingedPanther

WingedPanther

    A spammer's worst nightmare

  • Moderators
  • 16,831 posts
  • Location:Upstate, South Carolina
  • Programming Language:C, C++, PL/SQL, Delphi/Object Pascal, Pascal, Transact-SQL, Others
  • Learning:Java, C#, PHP, JavaScript, Lisp, Fortran, Haskell, Others
$array1 is not an element of $array2. In PHP, IIRC, a multi-dimensional array is an array of arrays.
Programming is a branch of mathematics.
My CodeCall Blog | My Personal Blog

#3
SoN9ne

SoN9ne

    Programmer

  • Members
  • PipPipPipPip
  • 129 posts
Thanks, I knew it was something simple. :)
"Life would be so much easier if we only had the source code."




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users