Solved

Using array for needle in_array ?

Posted on 2011-09-22
7
445 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
What Is Threat Intelligence?

Threat intelligence is often discussed, but rarely understood. Starting with a precise definition, along with clear business goals, is essential.

 
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

Find Ransomware Secrets With All-Source Analysis

Ransomware has become a major concern for organizations; its prevalence has grown due to past successes achieved by threat actors. While each ransomware variant is different, we’ve seen some common tactics and trends used among the authors of the malware.

Join & Write a Comment

Popularity Can Be Measured Sometimes we deal with questions of popularity, and we need a way to collect opinions from our clients.  This article shows a simple teaching example of how we might elect a favorite color by letting our clients vote for …
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to count occurrences of each item in an array.

705 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

20 Experts available now in Live!

Get 1:1 Help Now