I have problem with remove empty arrays. My array looks like:
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] =>
)
)
)
Last child always have two keys ( warehouse and price )
My code:
$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) );
Result:
Array
(
[Women] => Array
(
[S] => Array
(
[warehouse] => 22
[price] => 21212
)
)
[Men] => Array
(
[S] => Array
(
[warehouse] => 22
)
)
)
Its working but my problem is that the 'Men array' should be remove because last level have only warehouse key ( Correct two keys ). How change my function to get result who i want?