Very basic - if value is in an array

Posted on 2005-04-14
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
    LVL 4

    Expert Comment

    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.


    LVL 4

    Expert Comment

    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:


    LVL 25

    Accepted Solution

    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.
    LVL 9

    Expert Comment

    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

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

    Cheers =)

    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    What Security Threats Are You Missing?

    Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

    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…
    Part of the Global Positioning System A geocode ( is the major subset of a GPS coordinate (, the other parts being the altitude and t…
    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.

    737 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

    19 Experts available now in Live!

    Get 1:1 Help Now