PHP判断键值数组是否存在,使用empty或isset或array_key_exists
<?php$a = array('a'=>1, 'b'=>0, 'c'=>NULL);echo 'a test by empty: ' , empty($a['a']) ? 'not exist' : 'exist', PHP_EOL;echo 'a test by isset: ' , isset($a['a']) ? 'exist' : 'not exist', PHP_EOL;echo 'a test by array_key_exists: ' , array_key_exists('a', $a) ? 'exist' : 'not exist', PHP_EOL, PHP_EOL;echo 'b test by empty: ' , empty($a['b']) ? 'not exist' : 'exist', PHP_EOL;echo 'b test by isset: ' , isset($a['b']) ? 'exist' : 'not exist', PHP_EOL;echo 'b test by array_key_exists: ' , array_key_exists('b', $a) ? 'exist' : 'not exist', PHP_EOL, PHP_EOL;echo 'c test by empty: ' , empty($a['c']) ? 'not exist' : 'exist', PHP_EOL;echo 'c test by isset: ' , isset($a['c']) ? 'exist' : 'not exist', PHP_EOL;echo 'c test by array_key_exists: ' , array_key_exists('c', $a) ? 'exist' : 'not exist', PHP_EOL, PHP_EOL;?>
|
输出结果如下:
========================================================
a test by empty: exist
a test by isset: exist
a test by array_key_exists: exist
b test by empty: not exist
b test by isset: exist
b test by array_key_exists: exist
c test by empty: not exist
c test by isset: not exist
c test by array_key_exists: exist
========================================================
注意红色高亮部分
三种方式的语法区别
- empty: 参数为0或为NULL时(如上面列子),empty均返回TRUE,
- isset: 参数为NULL时,返回FALSE,0与NULL在PHP中是有区别的,isset(0)返回TRUE
- array_key_exists: 纯粹的判断数组键值对是否存在,无论值是多少
所以,从准确性的角度来看,array_key_exists是最准确的!
声明:此博文是摘抄他人文章,旨在方便查找,学习使用,
PHP判断键值数组是否存在,使用empty或isset或array_key_exists的更多相关文章
- PHP判断键值数组是否存在,使用empty或isset或array_key_exists(转)
一个例子 猜猜看,下面的例子会输出什么? 1 2 3 4 5 6 7 8 9 10 11 12 13 14 <?php $a = array('a'=>1, 'b'=>0, 'c'= ...
- codewars--js--Valid Braces--正则、键值数组
问题描述: Write a function that takes a string of braces, and determines if the order of the braces is v ...
- php 键值数组搜索查询
php 键值数组查询 ,需要先将其转换为纯数组,然后才能用in_array 进行搜索. $arr_combos = [ ['id' => '1001', 'value' => 'zs' ...
- 怎样理解数组的空元素empty与undefined的区别
数组的空元素empty表示空位, 它不是一种数据类型, 而是由于人为修改arr.length 或者写入时多写了逗号造成的. var arr = [1,2,3,4,,,5]; arr.length; a ...
- 判断数组中有没有某个键 isset 和 array_key_exists 的效率比较
判断数组中有没有某个键 isset 和 array_key_exists 谁的效率高呢? 使用 array_key_exists 代码如下 结果如下 如果使用 isset 代码如下 结果如下: 很明显 ...
- DWR3.0 dwr 返回值(数组,集合,Map)
首先导入项目所需要的包,如下:dwr.jar,commons-logging-1.0.4.jar,版本可以调整 1.web.xml<?xml version="1.0" en ...
- 【Android 应用开发】Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组
. 作者 : 万境绝尘 转载请注明出处 : http://blog.csdn.net/shulianghan/article/details/19913755 . 一. Android资源文件简介 1 ...
- Android资源文件 - 使用资源存储字符串 颜色 尺寸 整型 布尔值 数组
一. Android资源文件简介 1. Android应用资源的作用 (1) Android项目中文件分类 在Android工程中, 文件主要分为下面几类 : 界面布局文件, Java src源文件, ...
- 前端基础之javaScript(基本类型-布尔值数组-if-while)
目录 一:javaScript基本数据类型 1.字符串类型常用方法 2.返回长度 3.移出空白 4.移除左边的空白 5.移出右边的空格 6.返回第n个字符 7.子序列位置 8.根据索引获取子序列 9. ...
随机推荐
- Java Servlet(一):创建工程(jdk7+tomcat7+eclipse)
本篇文件主要记录下怎么在jdk7+tomcat7下,使用eclipse创建并运行一个servlet工程. 安装具体步骤从网上搜索就可以找到,这里不再赘述. 在eclipse中切换到j2ee下, 从导航 ...
- 试用windows Azure
试用windows Azure, 需要国外手机注册,信用卡注册. windows操作系统,只有2008R2,2012,2012R2可以选择,我选择XS最低档,然后选2012R2,欧洲数据中心,那个慢啊 ...
- https协议
- introcuding less css with less.js, using webcompiler ext
1.html [watch out for the !js load sequence and the attribute of !rel stylesheet/less!] <!DOCTYPE ...
- nyist 597 完数?
http://acm.nyist.net/JudgeOnline/problem.php?pid=597 完数? 时间限制:1000 ms | 内存限制:65535 KB 难度:1 描述 一个 ...
- 数据库 CRUD
1.删除表 drop table +表名 2.修改表 alter table+表名+ add(添加)+列名+ int(类型) alter table+表名+ drop(删除)+column(列) ...
- CCF真题之字符串匹配
201409-3 问题描述 给出一个字符串和多行文字,在这些文字中找到字符串出现的那些行.你的程序还需支持大小写敏感选项:当选项打开时,表示同一个字母的大写和小写看作不同的字符:当选项关闭时,表示同一 ...
- struts几个配置文件加载顺序_2015.01.04
struts几个配置文件加载顺序: 01:struts-default.xml 02:struts-plugin.xml 03:struts.xml 04:struts.properties 05:w ...
- .net 网站预编译命令
aspnet_compiler -v /Aspnet -p "C:\inetpub\wwwroot\a" C:\inetpub\wwwroot\a2 /Aspnet iis ...
- sql 索引创建
--格式 --CREATE [索引类型] INDEX 索引名称--ON 表名(列名)--WITH FILLFACTOR = 填充因子值0~100--GO ----------------------- ...