检查PHP数组中是否存在元素 [英] Check if element exists in PHP array

查看:94
本文介绍了检查PHP数组中是否存在元素的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个下面给出的数组:

I have an array which is given below:

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

我在if语句中使用以下代码看看数组中是否存在 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
    }

我在做什么错了?

推荐答案

它的数组在数组中。函数 array_key_exists 仅检查一个深度(如果密钥存在)。而且您的ked深2级,所以它不能返回true,因为只有键为 0。

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".

项目不是关键,而是价值;您必须使用函数 in_array array_search

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

这篇关于检查PHP数组中是否存在元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆