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
Solved

Using array for needle in_array ?

Posted on 2011-09-22
7
463 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
Active Directory Webinar

We all know we need to protect and secure our privileges, but where to start? Join Experts Exchange and ManageEngine on Tuesday, April 11, 2017 10:00 AM PDT to learn how to track and secure privileged users in Active Directory.

 
LVL 109

Expert Comment

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

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 109

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Part of the Global Positioning System A geocode (https://developers.google.com/maps/documentation/geocoding/) is the major subset of a GPS coordinate (http://en.wikipedia.org/wiki/Global_Positioning_System), the other parts being the altitude and t…
Password hashing is better than message digests or encryption, and you should be using it instead of message digests or encryption.  Find out why and how in this article, which supplements the original article on PHP Client Registration, Login, Logo…
Learn how to match and substitute tagged data using PHP regular expressions. Demonstrated on Windows 7, but also applies to other operating systems. Demonstrated technique applies to PHP (all versions) and Firefox, but very similar techniques will w…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…

829 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