Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Using array for needle in_array ?

Posted on 2011-09-22
7
Medium Priority
?
477 Views
Last Modified: 2012-05-12
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
Comment
Question by:tonyhhisc
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
rfportilla earned 2000 total points
ID: 36583169
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
 
LVL 9

Expert Comment

by:rfportilla
ID: 36583184
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
 

Author Closing Comment

by:tonyhhisc
ID: 36583566
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
Independent Software Vendors: 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!

 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36583581
See example 3 on this page:
http://php.net/in_array
0
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36583650
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
 
LVL 9

Expert Comment

by:rfportilla
ID: 36583791
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
 
LVL 111

Expert Comment

by:Ray Paseur
ID: 36583902
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

[Webinar] Lessons on Recovering from Petya

Skyport is working hard to help customers recover from recent attacks, like the Petya worm. This work has brought to light some important lessons. New malware attacks like this can take down your entire environment. Learn from others mistakes on how to prevent Petya like worms.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Build an array called $myWeek which will hold the array elements Today, Yesterday and then builds up the rest of the week by the name of the day going back 1 week.   (CODE) (CODE) Then you just need to pass your date to the function. If i…
These days socially coordinated efforts have turned into a critical requirement for enterprises.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to dynamically set the form action using jQuery.

688 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question