0

I'm trying to unset the array if the Array values are empty in the multidimensional array. Here's my Array:

Array
    (
        [main] => Array
            (
                [0] => Array
                    (
                        [title] => df
                        [link] => fdf
                    )
    
                [1] => Array
                    (
                        [title] => 
                        [link] => 
                    )
    
                [2] => Array
                    (
                        [title] => 
                        [link] => 
                    )
    
            )
    
    )

My PHP Code:

foreach($array as $key => $value){
  if(empty($value)){
      unset($menuDataJson[$key]);
  }
  
}

But it doesn't work...

2
  • 1
    array_filter() . Commented Dec 22, 2020 at 13:03
  • You do realise that that code is not processing the inner arrays, but just the outer main array?? Commented Dec 22, 2020 at 13:08

3 Answers 3

2

The problem is that you iterate over the root array, which has only one element 'main'. The following snippet should do the trick.

<?php

$array = [
    'main' => [
        [
            'title' => 'df',
            'link'  => 'fdf',
        ],
        [
            'title' => '',
            'link'  => '',
        ],
        [
            'title' => '',
            'link'  => '',
        ],
    ]
];


foreach ($array['main'] as $key => $value){
    if (empty($value['title'])){
        unset($array['main'][$key]);
    }
}

var_dump($array);
Sign up to request clarification or add additional context in comments.

2 Comments

That's your array structure, not sure if you can avoid it.
if you don't want to pass, or cannot pass ['main'], you could use this
0

try it:

foreach ($array['main'] as $key => $value){
    if (empty($value['title']) && empty($value['link'])){
        unset($array['main'][$key]);
    }
}

Comments

-1

The function array_filter will do that.

$result = array_filter($array);

However, if no callback function is specified, all empty entries of array will be removed, such as "" (an empty string), 0 (0 as an integer), 0.0 (0 as a float), "0" (0 as a string), NULL, FALSE and array() or [] (an empty array).

if you have this case, so add a callback function.

function customFilter($var)
{
    return ($var !== NULL && $var !== FALSE && $var !== "");
}

and use them for the 2nd parameter.

$result = array_filter($array, 'customFilter');  

If you have a multidimensional array, so is an enhancement necessary.

function array_filter_recursive($input, $callback = null)
{
    foreach ($input as &$value)
    {
        if (is_array($value))
        {
            $value = array_filter_recursive($value, $callback);
        }
    }
   
    return array_filter($input, $callback);
}

Use this function with your array like array_filter_recursive($array).

2 Comments

check my answer with a little modification it is now working
This will only operate on the outer level of the array (where there's one key, 'main'). Also, why reinvent the wheel when PHP has the empty function?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.