php:检查数组中的某些项目是否为空

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1616603/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-25 03:16:42  来源:igfitidea点击:

php: check if certain item in an array is empty

phparrays

提问by Nathaniel

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 中,如何检查数组中的指定项(按名称,我认为 - 数字也可能有效)是否为空?

回答by mauris

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

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

  • "" (an empty string)
  • 0 (0 as an integer)
  • "0" (0 as a string)
  • NULL
  • FALSE
  • array() (an empty array)
  • var $var; (a variable declared, but without a value in a class)
  • ""(空字符串)
  • 0(0 作为整数)
  • “0”(0 作为字符串)
  • 空值
  • 错误的
  • array()(空数组)
  • var $var; (声明的变量,但在类中没有值)

So take the example below:

所以看下面的例子:

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

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.

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

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

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

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

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

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

如果你想检查它是否为空,只需使用 empty() 函数。

回答by chaos

if(empty($array['item']))

or

或者

if(!isset($array['item']))

or

或者

if(!array_key_exists('item', $array))

depending on what preciselyyou mean by "empty". See the docs for empty(), isset() and array_key_exists()as to what exactly they mean.

这取决于正是你的意思是“空”。请参阅empty()isset()array_key_exists()的文档,了解它们的确切含义。

回答by Raf Chauhan

<?php

$myarray=array(1,5,6,5);

$anotherarray=array();

function checkEmpty($array){  
return (count($array)>0)?1:0;  
  }
echo checkEmpty($myarray);

echo checkEmpty($anotherarray);

?>

(for checking if empty result 1 else 0);

(用于检查是否为空结果 1 否则为 0);

Compactness is what I persue in my code.

紧凑性是我在代码中所追求的。

回答by user889030

i had such situation where i was getting tab it last index of array so if put things together then this might work for the most of cases

我遇到过这样的情况,我正在获取数组的最后一个索引,所以如果把东西放在一起,那么这可能适用于大多数情况

<?php 

if( ctype_space($array['index'])  && empty($array['index'])  && !isset($array['index']) ){
   echo 'array index is empty';
}else{
  echo 'Not empty';
}