简体   繁体   English

PHP-检查2D数组中的空元素

[英]PHP - Checking for empty element in 2D array

I have an array which looks like the following 我有一个看起来像下面的数组

array:2 [▼
  0 => array:1 [▼
    "input1" => "Something"
  ]
  1 => array:1 [▼
    "input2" => ""
  ]
]

Now the first element will always have some data. 现在,第一个元素将始终具有一些数据。 It is the second element I am interested in. At the moment, I am trying this 这是我感兴趣的第二个元素。此刻,我正在尝试

if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))
    var_dump("Some Data");
} else {
    var_dump("Both Empty");
}

The else should only be triggered if both elements are empty eg else应该仅在两个元素都为空时才触发,例如

array:2 [▼
  0 => array:1 [▼
    "input1" => ""
  ]
  1 => array:1 [▼
    "input2" => ""
  ]
]

If one of them have any data, the if should be triggered (so for the first array I showed, the if should be triggered). 如果其中一个有任何数据,则应触发if(因此,对于我显示的第一个数组,应触发if)。

How would I go about doing this, empty does not seem to work. 我将如何去做,空似乎没有用。

Thanks 谢谢

The 2nd level keys do not exist so you will always be told the values are empty. 第二级键不存在,因此将始终告诉您该值为空。 Change the line 换线

if(!empty($clientGroup[0][1]) || !empty($clientGroup[1][1]))

to, 至,

if(!empty($clientGroup[0]['input1']) || !empty($clientGroup[1]['input2']))

and you should get the results you're after. 并且您应该得到想要的结果。

It's not realy 2D array because you have associative array inside an other array. 这不是真正的2D数组,因为您在另一个数组中有关联数组。

you must use key name (input1, input2) to access the value. 您必须使用键名称(input1,input2)来访问该值。

I recommend to use 我建议使用

if($retourdata[0]["input1"] !== "" || $retourdata[1]["input2"] !== "")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM