Very basic - if value is in an array

Posted on 2005-04-14
Medium Priority
Last Modified: 2006-11-18

just getting to grips with PHP at the moment, but stumbled across this little bump in my path.

I have an array:

    [int_array1] => Array
            [0] => HHH
            [1] => GGG
            [2] => FFF
            [3] => EEE
            [4] =>CCC
            [5] =>BBB
            [6] => AAA
            [7] => E999
            [8] => E999

    [int_array2] => Array
            [0] => GGGG
            [1] => FFFF
            [2] => EEE
            [3] => DDD
            [4] => CCC
            [5] => BBB
            [6] =>AAA
            [7] => E999
            [8] => E999

My question is, how can I check if "int_array2" is within the main array called "main"??

For example, I've been messing with loads of different combination of this:

if (in_array(int_array1, $main)) {print "Found it";} else {print "No luck";}

That doesn't work.

Any help would be much appreciated!
Best regards,
Question by:noshankus

Expert Comment

ID: 13779715
It's because you need to quote the int_array1 in the in_array functions, e.g.

if (in_array('int_array1', $main)) { print "Found it"; } else { print "No Luck"; }

The spacing of { } etc is irrelevant just the way I code, in you're case you need to quote int_array1 as it's a string.  Hopefully this should work.



Expert Comment

ID: 13779723
It's worth mentioning that the first parameter of the in_array functions doesn't always have to be quoted, for example if your using number indexes etc.

You can read more about in_array here: http://uk.php.net/in_array


LVL 25

Accepted Solution

Marcus Bointon earned 200 total points
ID: 13779729
You need to look in the array keys, not the values, so you could either do:

if (in_array('int_array1', array_keys($main))) {print "Found it";} else {print "No luck";}

or a cleaner way:

if (array_key_exists('int_array1', $main)) {print "Found it";} else {print "No luck";}

You also need to quote string array index values or literal strings, otherwise you'll get warnings about undefined constants.

Expert Comment

ID: 13779749
A little confused as to your intentions... Do you:

a) want to know if there is a key called "int_value2" in the array $main, or,
b) want to know if the array $main contains the same values as in int_value2

if it's a) the solution is simple, use array_key_exists:

if (array_key_exists('int_array1', $main)) {print "Found it";} else {print "No luck";}

ShelfieldCollege, your solution didn't work for me using PHP 4.3.10

Hope this helps,


Author Comment

ID: 13779785
Thanks a million Squinky. Exactly what I was looking for!

Cheers =)

Featured Post

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.

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…
Originally, this post was published on Monitis Blog, you can check it here . In business circles, we sometimes hear that today is the “age of the customer.” And so it is. Thanks to the enormous advances over the past few years in consumer techno…
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…
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…
Suggested Courses
Course of the Month13 days, 17 hours left to enroll

807 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