0

I have this array

[4] => Array
    (
        [label] => Politics
        [value] => politics
    )

[5] => Array
    (
        [label] => 
        [value] => 
    )

[6] => Array
    (
        [label] => Sports
        [value] => sports
    )

I want delete this block

[5] => Array
    (
        [label] => 
        [value] => 
    )

So, I want delete process for empty array key or value.

Thanks.

2
  • what if label or value contain 0 ? Commented Sep 20, 2017 at 9:58
  • Possible duplicate of PHP : Remove object from array Commented Sep 20, 2017 at 9:59

7 Answers 7

1
$array = array(array('label' => "" ,'value' => ''),array('label' => 'test','value' => 'tset2'));
    foreach($array as $key => $val)
    {
        if(empty($val['label']) ||  empty($val['value']) )
        {
            unset($array[$key]);
        }
    }
    echo "<pre>";
    print_r($array) ;
    echo "</pre>";

i think this code will help you

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks so much, this is awesome.
For big arrays it's better to use built in functions instead of iterating with foreach because they work significantly faster.
0

Probably you can slice the element that you want to delete. Please refer link: Delete an element from an array

1 Comment

But, emtpy arrays is variable. It's not certain.
0
array_filter($your_table);  

http://php.net/manual/pl/function.array-filter.php

I used this when i had the same problem with empty values in array

Comments

0

You can use built in array_filter() function.

For this array structure:

$arr = [
    [
        'label' => 'Politics',
        'value' => 'politics'
    ],
    [
        'label' =>'',
        'value' => ''
    ],
    [
        'label' => 'Sports',
        'value' => 'sports'
    ],
];

This code would delete any element that doesn't have a label or a value:

$arr = array_filter($arr, function ($e) {
   return ($e['label'] || $e['value']);
});

Comments

0

I'd suggest doing something like the following pseudo-code

foreach (array_keys as key) {

   if  (empty(array[key][label])==true)
      unset(array[key]);
}

Comments

0

Try like this to '' filter,

$new = array_filter($entry, function ($var) {
    return ($var['label'] !== '' || $var['value'] !== '' );
});

print '<pre>';
print_r($new);
print '</pre>';

Edit: To null filter

$new = array_filter($entry, function ($var) {
    return ($var['label'] !== null || $var['value'] !== null );
});

print '<pre>';
print_r($new);
print '</pre>';

1 Comment

The issue is still same.
0

If you want to remove all those sub-arrays that have all null, empty or false values you can use array_filter with a callable array_filter.

$data = 
[
    [
        'foo' => null,
        'bar' => 'big'    
    ],
    [
    ],
    [
        'baz' => 'fat',
        'bat' => 'mama'
    ],
    [
        'man' => '',
        'qux' => null,
        'quux' => false
    ]
];

$data = array_filter($data, 'array_filter');

var_export($data);

Output:

array (
  0 => 
  array (
    'foo' => NULL,
    'bar' => 'big',
  ),
  2 => 
  array (
    'baz' => 'fat',
    'bat' => 'mama',
  ),
)

Using array_filter without a callback will discard elements equal to FALSE. ((bool) array() is false.)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.