phparraysvalidationmultidimensional-array

Determine if a 2d array containing a single row contains a specified column value


I have an array which is given below:

array(1) {
    [0]=> array(1) {
        ["type"]=> string(4) "item"
    }
} 

I'm using to the following code in an if statement to see if "item" exists in the array but it isn't been evaluated as true

if (array_key_exists('item', $_SESSION['type'])) 
{
    //do something
}

What am I doing wrong?


Solution

  • Its array in array. And function array_key_exists checks only one level deep, if the key exists. And your ked is 2 levels deep, so it cant return true, because there is key only "0".

    And "item" is not key, but value; you have to use function in_array or array_search.

    And also you should create your own function for that, because its array in array...