Fadi
Fadi

Reputation: 2370

how to remove nulls from the right side of array in php?

i have an array of the following elements

[null, null, 1, 4, 6, null, null, null, null]

how i can remove only the right side nulls from the array so it will become

[null, null, 1, 4, 6]

i tried array_filter but it's remove all the falsie values

$arr = array_filter([null, null, 1, 4, 6, null, null, null, null]);

the result was

$arr = [1, 4, 6]

Upvotes: 0

Views: 183

Answers (5)

Prafulla Kumar Sahu
Prafulla Kumar Sahu

Reputation: 9703

Another way can be

<?php
$array = [NULL, NULL, 1, 4, 6, NULL, NULL, NULL, NULL];

function trimRight($array) {
    $arr = $array;
    for ($i = count($array)-1; $i >= 0; $i--) {
        if( !is_null($array[$i])) {
            $arr = $array;
        } else {
            unset($array[$i]);
        }
    }
    return $arr;
}
var_dump(trimRight($array));
?>

Output

array(5) { [0]=> NULL [1]=> NULL [2]=> int(1) [3]=> int(4) [4]=> int(6) } 

Upvotes: 0

nice_dev
nice_dev

Reputation: 17835

You can make use of that array_filter result itself. You get all values that aren't NULL. So get the last key from that array and that is your end limit for array_slice function.

<?php

$arr = [null, null, 1, 4, 6, null, null, null, null,null];

$filtered_arr = array_filter($arr);
end($filtered_arr);
$limit = key($filtered_arr) + 1;
$arr = array_slice($arr,0,$limit);
print_r($arr);

Demo: https://3v4l.org/RVcjt

Update:

If the input array contains a 0, above answer could lead to incorrect results. To rectify the same, we can filter out only NULL values from the array.

<?php

$arr = [null, null, 1, 4, 6, null, 0, null, null,null];

$filtered_arr = array_filter($arr,function($value){
    return !is_null($value);
});
end($filtered_arr);
$limit = key($filtered_arr) + 1;
$arr = array_slice($arr,0,$limit);
print_r($arr);

Demo: https://3v4l.org/S5a96

Upvotes: 2

Ajith
Ajith

Reputation: 2666

You can try the below code

$notNull = true;
$item = [null, null, 1, 4, 6, null, null, null, null];
$output = array();
for($i=count($item)-1;$i>=0;$i--){

    if( is_null( $item[$i]) &&  $notNull ){
        continue;
    }
    else{
        $notNull = false;
        $output[] = $item[$i];  
    }
}
$output = array_reverse($output);

Demo Link

Upvotes: 1

Pezhvak
Pezhvak

Reputation: 10898

try this:

$arr = [null, null, 1, 4, 6, null, null, null, null];
$arr = array_reverse($arr);
foreach($arr as $a){
    if(is_null($a))
        array_shift($arr);
    else break;
}

$arr = array_reverse($arr);
print_r($arr);

it will give you the result you need:

array:5 [
  0 => null
  1 => null
  2 => 1
  3 => 4
  4 => 6
]

Upvotes: 0

Nigel Ren
Nigel Ren

Reputation: 57141

You could use array_pop() to remove items of the end till a non-null value is encountered (or an empty array) and then push the last item back on...

$array = [null, null, 1, 4, 6, null, null, null, null];

while( count($array) > 0 && ($end = array_pop($array)) === null );
array_push($array, $end);
print_r($array);

which gives...

Array
(
    [0] => 
    [1] => 
    [2] => 1
    [3] => 4
    [4] => 6
)

Upvotes: 0

Related Questions