我删除空数组时遇到问题。 我的数组看起来像:
canvas
最后一个孩子总是有两把钥匙(仓库和价格)
我的代码:
Array
(
[Women] => Array
(
[S] => Array
(
[warehouse] => 22
[price] => 21212
)
[M] => Array
(
[warehouse] =>
[price] =>
)
[L] => Array
(
[warehouse] =>
[price] =>
)
[XL] => Array
(
[warehouse] =>
[price] =>
)
[XXL] => Array
(
[warehouse] =>
[price] =>
)
)
[Men] => Array
(
[S] => Array
(
[warehouse] => 22
[price] =>
)
[M] => Array
(
[warehouse] =>
[price] =>
)
[L] => Array
(
[warehouse] =>
[price] =>
)
[XL] => Array
(
[warehouse] =>
[price] =>
)
[XXL] => Array
(
[warehouse] =>
[price] =>
)
)
)
结果:
$value = $this->$attribute;
$check = function($haystack) use(&$check)
{
foreach ($haystack as $key => $value) {
if (is_array($value)) {
$haystack[$key] = $check($haystack[$key]);
}
if (empty($haystack[$key])) {
unset($haystack[$key]);
}
}
return $haystack;
};
var_dump( $check($value) );
它的工作但我的问题是应该删除'Men数组',因为最后一级只有仓库密钥(正确的两个密钥)。 如何改变我的功能以获得我想要的结果?
答案 0 :(得分:0)
如果$ array包含这个``
strtok
然后迭代每个数组键女人,男人在每个键中我们将迭代到s,m,l,xl如果任何奖项缺少元素我们将取消它,并在最后迭代我们将检查女性和男性的大小,如果其中一个没有大小,那么我们将取消设置性别密钥。
Array
(
[Women] => Array
(
[S] => Array
(
[warehouse] => 22
[price] => 21212
)
[M] => Array
(
[warehouse] =>
[price] =>
)
[L] => Array
(
[warehouse] =>
[price] =>
)
[XL] => Array
(
[warehouse] =>
[price] =>
)
[XXL] => Array
(
[warehouse] =>
[price] =>
)
)
[Men] => Array
(
[S] => Array
(
[warehouse] => 22
[price] =>
)
[M] => Array
(
[warehouse] =>
[price] =>
)
[L] => Array
(
[warehouse] =>
[price] =>
)
[XL] => Array
(
[warehouse] =>
[price] =>
)
[XXL] => Array
(
[warehouse] =>
[price] =>
)
)
)