0

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]
2
  • How you are separating right and left side ? Commented Dec 12, 2019 at 10:49
  • after the real number is the right side so after 6 Commented Dec 12, 2019 at 10:51

5 Answers 5

2

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

Sign up to request clarification or add additional context in comments.

2 Comments

As pointed out elsewhere - this answer won't work with [null, null, 1, 4, 6, null, 0, null, null,null] as it gives the same result as it currently does.
@Fadi Please look at the updated code just in case your input array could have zero values.
1

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

Comments

0

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
)

Comments

0

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
]

Comments

0

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) } 

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.