Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 484
  • Last Modified:

Using array for needle in_array ?

PHP says: "Changelog:    4.2.0       needle may now be an array. "

So I'm thinking I can search an array for values matching any one of the values in MY array.

For example:

<?
$needles = array("red","blue","green","yellow");

$haystack = array("blue","yellow");

if (in_array($needles,$haystack)) { echo "in array"; } else { echo "not in array"; }
?>

Open in new window


I thought this would echo "in array" since blue and yellow are found in $haystack. But it doesn't.

So, maybe I was wrong... is there a way to do this without using a loop? Hopefully I'm just not doing the above in the correct way... Help?

Thanks!!
0
tonyhhisc
Asked:
tonyhhisc
  • 3
  • 3
1 Solution
 
rfportillaCommented:
You won't be able to directly search an array with an array.  You will have to use a foreach loop.  Without testing, it would looking something like this:

foreach ($needles as $needle) {
  echo in_array($needle,$haystack)
}
0
 
rfportillaCommented:
Don't be afraid of the manual. It's wonderfully helpful.

inarray: http://php.net/manual/en/function.in-array.php
foreach: http://us3.php.net/manual/en/control-structures.foreach.php
0
 
tonyhhiscAuthor Commented:
Thanks a lot!

And I did RTFM, that's why I quoted the first line from there.

Unfortunately, the manual is not always the end-all answer since the manual fails to explain itself clearly all the time. "needle may now be an array." means just that to me, NEEDLE CAN NOW BE AN ARRAY.

And, I already knew how to use foreach, so not sure how that manual entry would have helped... :)
0
Concerto's Cloud Advisory Services

Want to avoid the missteps to gaining all the benefits of the cloud? Learn more about the different assessment options from our Cloud Advisory team.

 
Ray PaseurCommented:
See example 3 on this page:
http://php.net/in_array
0
 
Ray PaseurCommented:
You can see this script run on my server, here.
http://www.laprbass.com/RAY_temp_tonyhisc.php

HTH, ~Ray
<?php // RAY_temp_tonyhisc.php
error_reporting(E_ALL);
echo "<pre>";

// DEFINE THE TEST DATA
$needles  = array("red","blue","green","yellow");
$haystack = array("blue","yellow");

// SHOW THE TEST DATA
echo "NEEDLES ";
print_r($needles);
echo PHP_EOL;
echo "HAYSTACK ";
print_r($haystack);
echo PHP_EOL;

// TEST THE HYPOTHESIS
if (in_array($needles,$haystack)) { echo "in array"; } else { echo "not in array"; }
echo PHP_EOL;


// DEFINE SOME NEW TEST DATA
$needles  = array("blue","yellow");

$haystack  = array
( array("blue","yellow")
, array("red", "green")
)
;

// SHOW THE NEW TEST DATA
echo "NEEDLES ";
print_r($needles);
echo PHP_EOL;
echo "HAYSTACK ";
print_r($haystack);
echo PHP_EOL;


// TEST THE HYPOTHESIS WITH THE NEW DATA
if (in_array($needles,$haystack)) { echo "in NEW array"; } else { echo "not in NEW array"; }
echo PHP_EOL;

Open in new window

0
 
rfportillaCommented:
Sorry, we get people with all types of experience here.  I try not to say RTFM, lol, but sometimes it is appropriate.

I see your point about the array usage in 4.2.0.  The 3rd example supports what you are trying to do.  However, when it doesn't work simply the way you want it to, you might as well use the next best thing.  I may have to look back at this later.  Good luck!

0
 
Ray PaseurCommented:
I believe the manual is correct.  The needle can be an array.  But for the needle to match something in the haystack, the thing in the haystack needs to be the same data type as the needle.  So if the needle is a string, the haystack needs to be an array of strings.  And if the needle is an array, the haystack needs to be an array of arrays.

That's why the second example here works correctly and the first does not.
http://www.laprbass.com/RAY_temp_tonyhisc.php
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 3
  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now