Php check multiple array key exists

741 votes

3 answers

Php check multiple array key exists

Php check multiple array key exists

Php check multiple array key exists

Get the solution ↓↓↓

Is there a simpler, smarter and clearer way to write this?

if (array_key_exists("name", $array) AND array_key_exists("age", $array) AND array_key_exists("size", $array) AND array_key_exists("gender", $array) {
    echo "Keys exist!";
}

2022-05-11




168

votes

Php check multiple array key exists

Php check multiple array key exists

Answer

Solution:

Use simply isset(), a language construct.

Language constructs are much more faster than functions.

Explanation:

1) Create an array of 4 keys you need to find.

2) Loop over this array and useisset() to find out if the key (element) exists.

Undefined answered

2022-05-11

Link to answer




465

votes

Php check multiple array key exists

Php check multiple array key exists

Answer

Solution:

use can also use this way..


Undefined answered

2022-05-11

Link to answer




666

votes

Php check multiple array key exists

Php check multiple array key exists

Answer

Solution:

You can do it like this:

'value1',
    'index2'=>'value2',
    'index3'=>'value3'
);

$indexesToSearch = array('index1', 'index2');

if(count(array_intersect(array_keys($array), $indexesToSearch)) == count($indexesToSearch))
{
    // ... do something
}

Undefined answered

2022-05-11

Link to answer




People are also looking for solutions of the problem: call to undefined function str_contains()
Source

Share


Didn't find the answer?

Our community is visited by hundreds of web development professionals every day. Ask your question and get a quick answer for free.


Similar questions

Find the answer in similar questions on our website.

View Discussion

Improve Article

Save Article

  • Read
  • Discuss
  • View Discussion

    Improve Article

    Save Article

    In this article, we will see how to get the array key using the array_key_exists() function in PHP, & will also see its implementation through the example. The array_key_exists() is an inbuilt function of PHP that is used to check whether a specific key or index is present inside an array or not. The function returns true if the specified key is found in the array otherwise returns false. The required key while specifying the array, is skipped then it will generate the integer value for the key, starting from 0 that will be incremented by 1 for each value.

    Pre-requisite: PHP array_keys() Function

    Syntax:

    boolean array_key_exists($index, $array)

    Parameters: This function takes 2 arguments and is described below:

    • $index: This parameter is mandatory and refers to the key that is needed to be searched for in an input array.
    • $array: This parameter is mandatory and refers to the original array in which we want to search the given key $index.

    Return Value: This function returns a boolean value i.e., TRUE and FALSE depending on whether the key is present in the array or not respectively.

    Note: Nested keys will return the FALSE result. 

    Example 1: The below programs illustrates the array_key_exists() function in PHP. Here, we will see how we can find a key inside an array that holds key_value pair.

    PHP

        function Exists($index, $array)

        {

            if (array_key_exists($index, $array))

            {

                echo "Found the Key";

            }

            else

            {

                echo "Key not Found";

            }

        }

        $array = array(

            "ram" => 25,

            "krishna" => 10,

            "aakash" => 20,

            "gaurav"

        );

        $index = "aakash";

        print_r(Exists($index, $array));

    ?>

    Output:

    Found the Key

    If no key_value pair exits, as shown in the below case, then the array takes the default keys i.e. numeric keys starting from zero, into consideration and returns true as far as the $index limit ranges.

    Example: This example illustrates the array_key_exists() function in PHP by specifying the particular $index value.

    PHP

        function Exists($index, $array)

        {

            if (array_key_exists($index, $array))

            {

                echo "Found the Key";

            }

            else

            {

                echo "Key not Found";

            }

        }  

        $array = array(

            "ram",

            "krishna",

            "aakash",

            "gaurav"

        );

        $index = 2;

        print_r(Exists($index, $array));

    ?>

    Output:

    Found the Key

    Reference: http://php.net/manual/en/function.array-key-exists.php


    How do you check if a key exists in an array in PHP?

    PHP array_key_exists() Function The array_key_exists() function checks an array for a specified key, and returns true if the key exists and false if the key does not exist.

    How do I check if an array contains multiple values in PHP?

    php check if any of multiple values in array.
    function in_array_any($needles, $haystack) {.
    return ! empty(array_intersect($needles, $haystack));.
    echo in_array_any( [3,9], [5,8,3,1,2] ); // true, since 3 is present..
    echo in_array_any( [4,9], [5,8,3,1,2] ); // false, neither 4 nor 9 is present..

    How do I find associative array in PHP?

    How to check if PHP array is associative or sequential? There is no inbuilt method in PHP to know the type of array. If the sequential array contains n elements then their index lies between 0 to (n-1). So find the array key value and check if it exist in 0 to (n-1) then it is sequential otherwise associative array.

    How do you find array keys?

    The array_keys() function is used to get all the keys or a subset of the keys of an array. Note: If the optional search_key_value is specified, then only the keys for that value are returned. Otherwise, all the keys from the array are returned. Specified array.