Solved

Using array for needle in_array ?

Posted on 2011-09-22
7
450 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
  • 3
  • 3
7 Comments
 
LVL 9

Accepted Solution

by:
rfportilla earned 500 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
LVL 108

Expert Comment

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

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 108

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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
Introduction This article is intended for those who are new to PHP error handling (https://www.experts-exchange.com/articles/11769/And-by-the-way-I-am-New-to-PHP.html).  It addresses one of the most common problems that plague beginning PHP develop…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …

910 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now