1

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?

2

1 Answer 1

0

if $array is containing this
`

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] => 
            )
      )
)


Then iterate each array key women, men in each of that key we will iterate to s,m,l,xl if any of the prize has missing elements we will unset it, and at last of iteration we will check the size of women and men, if one of them dont have size then we will unset the gender key.

foreach($array as $gender => $array_val){
  foreach($gender as $size=>$size_val){
    if(empty($size_val)){
      unset($array[$gender][$size]);
    }
  }
  if(count($array[$gender])==0) unset($array[$gender]);
}
var_dump($array);
Sign up to request clarification or add additional context in comments.

2 Comments

I think you don't understand me. If last parent both params (warehouse and price) is empty then array must be unset
Sorry,the array sized [s] on men is removed then?