php:检查数组中的某些项目是否为空 [英] php: check if certain item in an array is empty

查看:29
本文介绍了php:检查数组中的某些项目是否为空的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在 PHP 中,如何检查数组中的指定项(按名称,我认为 - 数字也可能有效)是否为空?

In PHP, how would one check to see if a specified item (by name, I think - number would probably also work) in an array is empty?

推荐答案

空类型(来自 PHP 手册).以下对于任何变量都被认为是空的:

Types of empty (from PHP Manual). The following are considered empty for any variable:

  • ""(空字符串)
  • 0(0 作为整数)
  • "0" (0 作为字符串)
  • 错误
  • array()(空数组)
  • var $var;(声明了一个变量,但在类中没有值)

请看下面的例子:

$arr = array(
          'ele1' => 'test',
          'ele2' => false           
       );

1) $arr['ele3'] 未设置.所以:
isset($arr['ele3']) === false &&empty($arr['ele3']) === true
它未设置且为空.empty() 检查变量是否已设置且是否为空.

1) $arr['ele3'] is not set. So:
isset($arr['ele3']) === false && empty($arr['ele3']) === true
it is not set and empty. empty() checks for whether the variable is set and empty or not.

2) $arr['ele2'] 已设置,但为空.所以:
isset($arr['ele2']) === true &&empty($arr['ele2']) === true

2) $arr['ele2'] is set, but empty. So:
isset($arr['ele2']) === true && empty($arr['ele2']) === true

1) $arr['ele1'] 已设置且不为空:
isset($arr['ele1']) === true &&empty($arr['ele1']) === false

1) $arr['ele1'] is set and not empty:
isset($arr['ele1']) === true && empty($arr['ele1']) === false

如果您想检查它是否为空,只需使用 empty() 函数即可.

if you wish to check whether is it empty, simply use the empty() function.

这篇关于php:检查数组中的某些项目是否为空的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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