Remove NULL, FALSE, and '' - but not 0 - from a PHP array

后端 未结 9 2216
一生所求
一生所求 2020-12-09 02:15

I want to remove NULL, FALSE and \'\' values .

I used array_filter but it removes the 0\' s also.

相关标签:
9条回答
  • 2020-12-09 02:30

    Use a custom callback function with array_filter. See this example, lifted from PHP manual, on how to use call back functions. The callback function in the example is filtering based on odd/even; you can write a little function to filter based on your requirements.

    <?php
    function odd($var)
    {
        // returns whether the input integer is odd
        return($var & 1);
    }
    
    function even($var)
    {
        // returns whether the input integer is even
        return(!($var & 1));
    }
    
    $array1 = array("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
    $array2 = array(6, 7, 8, 9, 10, 11, 12);
    
    echo "Odd :\n";
    print_r(array_filter($array1, "odd"));
    echo "Even:\n";
    print_r(array_filter($array2, "even"));
    ?> 
    
    0 讨论(0)
  • 2020-12-09 02:31
    function ExtArray($linksArray){
        foreach ($linksArray as $key => $link)
        {
            if ($linksArray[$key] == '' || $linksArray[$key] == NULL || $linksArray[$key] == FALSE || $linksArray[$key] == '')
            {
                unset($linksArray[$key]);
            }else {
                return $linksArray[$key];
            }
        }
    }
    

    This function may help you

    0 讨论(0)
  • 2020-12-09 02:34

    One-liners are always nice.

    $clean_array = array_diff(array_map('trim', $my_array), array('', NULL, FALSE));
    

    Explanation:

    • 1st parameter of array_diff: The trimmed version of $my_array. Using array_map, surrounding whitespace is trimmed from every element via the trim function. It is good to use the trimmed version in case an element contains a string that is nothing but whitespace (i.e. tabs, spaces), which I assume would also want to be removed. You could just as easily use $my_array for the 1st parameter if you don't want to trim the elements.
    • 2nd parameter of array_diff: An array of items that you would like to remove from $my_array.
    • Output: An array of elements that are contained in the 1st array that are not also contained in the 2nd array. In this case, because '', NULL, and FALSE are within the 2nd array, they can never be returned by array_diff.

    EDIT:

    It turns out you don't need to have NULL and FALSE in the 2nd array. Instead you can just have '', and it will work the same way:

    $clean_array = array_diff(array_map('trim', $my_array), array(''));
    
    0 讨论(0)
  • 2020-12-09 02:34

    check whether it is less than 1 and greater than -1 if then dont remove it...

    $arrayValue = (NULL,FALSE,'',0,1);
    $newArray = array();
    foreach($arrayValue as $value) {
        if(is_int($value) || ($value>-1 && $value <1)) {
            $newArray[] = $value;
        }
    }
    
    print_r($newArray);
    
    0 讨论(0)
  • 2020-12-09 02:40

    array_filter doesn't work because, by default, it removes anything that is equivalent to FALSE, and PHP considers 0 to be equivalent to false. The PHP manual has this to say on the subject:

    When converting to boolean, the following values are considered FALSE:

    • the boolean FALSE itself
    • the integer 0 (zero)
    • the float 0.0 (zero)
    • the empty string, and the string "0"
    • an array with zero elements
    • an object with zero member variables (PHP 4 only)
    • the special type NULL (including unset variables)
    • SimpleXML objects created from empty tags

    Every other value is considered TRUE (including any resource).

    You can pass a second parameter to array_filter with a callback to a function you write yourself, which tells array_filter whether or not to remove the item.

    Assuming you want to remove all FALSE-equivalent values except zeroes, this is an easy function to write:

    function RemoveFalseButNotZero($value) {
      return ($value || is_numeric($value));
    }
    

    Then you just overwrite the original array with the filtered array:

    $array = array_filter($array, "RemoveFalseButNotZero");
    
    0 讨论(0)
  • 2020-12-09 02:41
    function my_filter($var)
    {
        // returns values that are neither false nor null (but can be 0)
        return ($var !== false && $var !== null && $var !== '');
    }
    
    $entry = array(
                 0 => 'foo',
                 1 => false,
                 2 => -1,
                 3 => null,
                 4 => '',
                 5 => 0
              );
    
    print_r(array_filter($entry, 'my_filter'));
    

    Outputs:

    Array
    (
        [0] => foo
        [2] => -1
        [5] => 0
    )
    
    0 讨论(0)
提交回复
热议问题