1
$fav = explode("|","0 | 1 | 2 | ");  
print_r($fav);  
$fav = array_pop($fav);  
echo "<br>after <br>";  
print_r($fav);  

what's the problem in my code? i want to remove the last value in the array $fav.

6 Answers 6

7

array_pop returns last value not remaining part.

so change this line $fav = array_pop($fav); to array_pop($fav);

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

Comments

3

Remove the assigment, so it looks like this:

array_pop($fav);  

array_pop returns the removed value and modifies the array in-place, so you must not assign the return value to the array variable.

Comments

2
    <?php  
    $fav = explode("|","0 | 1 | 2 | ");  
    print_r($fav);  
    $remove_last= array_pop($fav);  
    echo "<br>after <br>";  
    print_r($fav);  
    ?>


    output

Array
    (
        [0] => 0 
        [1] =>  1 
        [2] =>  2 
        [3] =>  
    )
    after
Array
    (
        [0] => 0 
        [1] =>  1 
        [2] =>  2 
    )

Comments

1

You're assigning the result of array_pop over the original array. Change that line to:

$removed = array_pop($fav);

Comments

1

you've overwritten the variable $fav. you also might want to remove the last | from your string that you explode.

<?php  
$fav = explode("|","0 | 1 | 2");  
print_r($fav);  // output should be: 0, 1, 2
$last_element = array_pop($fav);  
echo "<br>after <br>";  
print_r($fav);  // output should be: 0, 1
?>

Comments

1

array_pop returns the remove element, not the array itself.

try this:

<pre>  
<?php  
$fav = explode("|","0 | 1 | 2 | ");  
print_r($fav);  
$last = array_pop($fav);  
echo "<br>after <br>";  
print_r($fav);  
?>  
</pre> 

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.