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.

blueway
  • 129
  • 7

7 Answers7

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

Subhash Shipu
  • 343
  • 1
  • 4
  • 15
0

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

Tavish Aggarwal
  • 1,020
  • 3
  • 22
  • 51
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

pr1nc3
  • 8,108
  • 3
  • 23
  • 36
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>';
A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
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']);
});
Pepe Bolo
  • 71
  • 4
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]);
}
Chris J
  • 1,527
  • 14
  • 19
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.)

Progrock
  • 7,373
  • 1
  • 19
  • 25