Remove empty array elements

前端 未结 27 3400
半阙折子戏
半阙折子戏 2020-11-21 23:17

Some elements in my array are empty strings based on what the user has submitted. I need to remove those elements. I have this:

foreach($linksArray as $link)         


        
相关标签:
27条回答
  • 2020-11-21 23:46
    $a = array(1, '', '', '', 2, '', 3, 4);
    $b = array_values(array_filter($a));
    
    print_r($b)
    
    0 讨论(0)
  • 2020-11-21 23:46

    I think array_walk is much more suitable here

    $linksArray = array('name', '        ', '  342', '0', 0.0, null, '', false);
    
    array_walk($linksArray, function(&$v, $k) use (&$linksArray){
        $v = trim($v);
        if ($v == '')
            unset($linksArray[$k]);
    });
    print_r($linksArray);
    

    Output:

    Array
    (
        [0] => name
        [2] => 342
        [3] => 0
        [4] => 0
    )
    
    • We made sure that empty values are removed even if the user adds more than one space

    • We also trimmed empty spaces from the valid values

    • Finally, only (null), (Boolean False) and ('') will be considered empty strings

    As for False it's ok to remove it, because AFAIK the user can't submit boolean values.

    0 讨论(0)
  • 2020-11-21 23:47

    Another one liner to remove empty ("" empty string) elements from your array.

    $array = array_filter($array, function($a) {return $a !== "";});
    

    Note: This code deliberately keeps null, 0 and false elements.


    Or maybe you want to trim your array elements first:

    $array = array_filter($array, function($a) {
        return trim($a) !== "";
    });
    

    Note: This code also removes null and false elements.

    0 讨论(0)
  • 2020-11-21 23:48

    You can use array_filter to remove empty elements:

    $emptyRemoved = array_filter($linksArray);
    

    If you have (int) 0 in your array, you may use the following:

    $emptyRemoved = remove_empty($linksArray);
    
    function remove_empty($array) {
      return array_filter($array, '_remove_empty_internal');
    }
    
    function _remove_empty_internal($value) {
      return !empty($value) || $value === 0;
    }
    

    EDIT: Maybe your elements are not empty per se but contain one or more spaces... You can use the following before using array_filter

    $trimmedArray = array_map('trim', $linksArray);
    
    0 讨论(0)
  • 2020-11-21 23:48
    foreach($arr as $key => $val){
       if (empty($val)) unset($arr[$key];
    }
    
    0 讨论(0)
  • 2020-11-21 23:51

    In short:

    This is my suggested code:

    $myarray =  array_values(array_filter(array_map('trim', $myarray), 'strlen'));
    

    Explanation:

    I thinks use array_filter is good, but not enough, because values be like space and \n,... keep in the array and this is usually bad.

    So I suggest you use mixture ‍‍array_filter and array_map.

    array_map is for trimming, array_filter is for remove empty values, strlen is for keep 0 value, and array_values is for re indexing if you needed.

    Samples:

    $myarray = array("\r", "\n", "\r\n", "", " ", "0", "a");
    
    // "\r", "\n", "\r\n", " ", "a"
    $new1 = array_filter($myarray);
    
    // "a"
    $new2 = array_filter(array_map('trim', $myarray));
    
    // "0", "a"
    $new3 = array_filter(array_map('trim', $myarray), 'strlen');
    
    // "0", "a" (reindex)
    $new4 = array_values(array_filter(array_map('trim', $myarray), 'strlen'));
    
    var_dump($new1, $new2, $new3, $new4);
    

    Results:

    array(5) {
      [0]=>
    " string(1) "
      [1]=>
      string(1) "
    "
      [2]=>
      string(2) "
    "
      [4]=>
      string(1) " "
      [6]=>
      string(1) "a"
    }
    array(1) {
      [6]=>
      string(1) "a"
    }
    array(2) {
      [5]=>
      string(1) "0"
      [6]=>
      string(1) "a"
    }
    array(2) {
      [0]=>
      string(1) "0"
      [1]=>
      string(1) "a"
    }
    

    Online Test:

    http://phpio.net/s/5yg0

    0 讨论(0)
提交回复
热议问题