检查空数组的最佳方法?
如何递归地检查数组中是否有空内容,如下例所示:
Array
(
[product_data] => Array
(
[0] => Array
(
[title] =>
[description] =>
[price] =>
)
)
[product_data] => Array
(
[1] => Array
(
[title] =>
[description] =>
[price] =>
)
)
)
数组不为空,但没有内容。我如何用一个简单的函数来检查这个?
感谢!!
How can I check an array recursively for empty content like this example:
Array
(
[product_data] => Array
(
[0] => Array
(
[title] =>
[description] =>
[price] =>
)
)
[product_data] => Array
(
[1] => Array
(
[title] =>
[description] =>
[price] =>
)
)
)
The array is not empty but there is no content. How can I check this with a simple function?
Thank!!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(11)
如果您的数组只有一层深,您也可以这样做:
在大多数情况下有效:)
If your array is only one level deep you can also do:
Works in most cases :)
array_walk_recursive 的解决方案:
Solution with array_walk_recursive:
假设数组始终包含相同类型的数据:
Assuming the array will always contain the same type of data:
包括短路。
Short circuiting included.
这是我的版本。一旦它在数组中找到非空字符串,它就会停止。另外,它会正确检查空字符串,因此 0(零)不会被视为空字符串(如果您使用empty() 函数,则会被视为空字符串)。顺便说一句,多年来,即使仅对字符串使用此函数也已被证明是无价的。
Here's my version. Once it finds a non-empty string in an array, it stops. Plus it properly checks on empty strings, so that a 0 (zero) is not considered an empty string (which would be if you used empty() function). By the way even using this function just for strings has proven invaluable over the years.
如果有人偶然发现这个问题并且需要检查整个数组是否为 NULL,这意味着数组中的每一对都等于 null,那么这是一个方便的函数。如果任何变量也返回 NULL,您可以很容易地将其修改为返回 true。我需要这个用于更新用户数据的某个 Web 表单,并且它可能完全空白,因此不需要执行任何 SQL。
If anyone stumbles on this question and needs to check if the entire array is NULL, meaning that each pair in the array is equal to null, this is a handy function. You could very easily modify it to return true if any variable returns NULL as well. I needed this for a certain web form where it updated users data and it was possible for it to come through completely blank, therefor not needing to do any SQL.
如果传递的变量不是数组,或者任何嵌套数组包含值(包括假值!),则返回 TRUE。否则返回
FALSE
。短路。
Returns
TRUE
if passed a variable other than an array, or if any of the nested arrays contains a value (including falsy values!). ReturnsFALSE
otherwise.Short circuits.
这是一个很好的实用函数,如果数组为空,则返回 true (1),否则返回 false (0):
例如,给定一个多维数组:
You'将获得从
is_array_empty()
返回的true
值,因为没有设置任何值:以交互方式查看此代码:http://codepad.org/l2C0Efab
Here's a good utility function that will return
true (1)
if the array is empty, orfalse (0)
if not:For example, given a multidimensional array:
You'll get a
true
value returned fromis_array_empty()
, since there are no values set:View this code interactively at: http://codepad.org/l2C0Efab
我需要一个函数来递归地过滤数组中的非空值。
这是我的递归函数:
使用参数
$keepNonArrayValues
,您可以决定值是否为0
(数字零)、''
(空字符串)或false
(bool FALSE) 被保留在数组中。换句话说:如果$keepNonArrayValues = true
则只会从目标数组中删除空数组。array_slice($result, 0)
的作用是重新排列数字索引 (0..length-1)。此外,通过此函数过滤数组后,可以使用
empty($filterredArray)
进行测试。I needed a function to filter an array recursively for non empty values.
Here is my recursive function:
With parameter
$keepNonArrayValues
you can decide if values such0
(number zero),''
(empty string) orfalse
(bool FALSE) shout be kept in the array. In other words: if$keepNonArrayValues = true
only empty arrays will be removed from target array.array_slice($result, 0)
has the effect that numeric indices will be rearranged (0..length-1).Additionally, after filtering the array by this function it can be tested with
empty($filterredArray)
.