开发者

PHP array_pop is not working

$fav = explod开发者_如何学运维e("|","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.


array_pop returns last value not remaining part.

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


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.


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


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

$removed = array_pop($fav);


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
?>


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> 
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜