com*_*mod 13 php arrays recursion
如何以递归方式检查数组中的空内容,如下例所示:
Array
(
[product_data] => Array
(
[0] => Array
(
[title] =>
[description] =>
[price] =>
)
)
[product_data] => Array
(
[1] => Array
(
[title] =>
[description] =>
[price] =>
)
)
)
Run Code Online (Sandbox Code Playgroud)
该数组不是空的,但没有内容.如何通过简单的功能检查?
谢谢!!
emu*_*ano 16
function is_array_empty($InputVariable)
{
$Result = true;
if (is_array($InputVariable) && count($InputVariable) > 0)
{
foreach ($InputVariable as $Value)
{
$Result = $Result && is_array_empty($Value);
}
}
else
{
$Result = empty($InputVariable);
}
return $Result;
}
Run Code Online (Sandbox Code Playgroud)
mar*_*out 10
如果你的数组只有一个级别,你也可以这样做:
if (strlen(implode('', $array)) == 0)
Run Code Online (Sandbox Code Playgroud)
适用于大多数情况:)
小智 7
使用array_walk_recursive的解决方案:
function empty_recursive($value)
{
if (is_array($value)) {
$empty = TRUE;
array_walk_recursive($value, function($item) use (&$empty) {
$empty = $empty && empty($item);
});
} else {
$empty = empty($value);
}
return $empty;
}
Run Code Online (Sandbox Code Playgroud)