Remove empty array elements

前端 未结 27 3397
半阙折子戏
半阙折子戏 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:40
    $my = ("0"=>" ","1"=>"5","2"=>"6","3"=>" ");   
    
    foreach ($my as $key => $value) {
        if (is_null($value)) unset($my[$key]);
    }
    
    foreach ($my as $key => $value) {
        echo   $key . ':' . $value . '<br>';
    } 
    

    output

    1:5

    2:6

    0 讨论(0)
  • 2020-11-21 23:41
    $out_array = array_filter($input_array, function($item) 
    { 
        return !empty($item['key_of_array_to_check_whether_it_is_empty']); 
    }
    );
    
    0 讨论(0)
  • 2020-11-21 23:41

    Just want to contribute an alternative to loops...also addressing gaps in keys...

    In my case I wanted to keep sequential array keys when the operation was complete (not just odd numbers, which is what I was staring at. Setting up code to look just for odd keys seemed fragile to me and not future-friendly.)

    I was looking for something more like this: http://gotofritz.net/blog/howto/removing-empty-array-elements-php/

    The combination of array_filter and array_slice does the trick.

    $example = array_filter($example); $example = array_slice($example,0);

    No idea on efficiencies or benchmarks but it works.

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

    I use the following script to remove empty elements from an array

    for ($i=0; $i<$count($Array); $i++)
      {
        if (empty($Array[$i])) unset($Array[$i]);
      }
    
    0 讨论(0)
  • 2020-11-21 23:44

    The most voted answer is wrong or at least not completely true as the OP is talking about blank strings only. Here's a thorough explanation:

    What does empty mean?

    First of all, we must agree on what empty means. Do you mean to filter out:

    1. the empty strings only ("")?
    2. the strictly false values? ($element === false)
    3. the falsey values? (i.e. 0, 0.0, "", "0", NULL, array()...)
    4. the equivalent of PHP's empty() function?

    How do you filter out the values

    To filter out empty strings only:

    $filtered = array_diff($originalArray, array(""));
    

    To only filter out strictly false values, you must use a callback function:

    $filtered = array_diff($originalArray, 'myCallback');
    function myCallback($var) {
        return $var === false;
    }
    

    The callback is also useful for any combination in which you want to filter out the "falsey" values, except some. (For example, filter every null and false, etc, leaving only 0):

    $filtered = array_filter($originalArray, 'myCallback');
    function myCallback($var) {
        return ($var === 0 || $var === '0');
    }
    

    Third and fourth case are (for our purposes at last) equivalent, and for that all you have to use is the default:

    $filtered = array_filter($originalArray);
    
    0 讨论(0)
  • 2020-11-21 23:44

    As per your method, you can just catch those elements in an another array and use that one like follows,

    foreach($linksArray as $link){
       if(!empty($link)){
          $new_arr[] = $link
       }
    }
    
    print_r($new_arr);
    
    0 讨论(0)
提交回复
热议问题