开发者

array_shift not removing the first element of the array

I have the following code

    echo '<pre>';
    print_r($this->region_id);
    echo '</pre>';


    if(end($this->region_id) != 0){
        if($this->region_id[0] == 0){
            array_shift($this->region_id); 开发者_Python百科 
        }
    }

    echo '<pre>';
    print_r($this->region_id);
    echo '</pre>';

Somehow it's not removing the first element of the array, since my results look exactly the same after the code runs with the print_r

Array
(
    [0] => 0
    [1] => 30
    [2] => 14
)

Array
(
    [0] => 0
    [1] => 30
    [2] => 14
)

The code does reach the array shift.


What's wrong with doing just this

echo '<pre>';
print_r($this->region_id);
echo '</pre>';

        array_shift($this->region_id);  

echo '<pre>';
print_r($this->region_id);
echo '</pre>';


Silly me. :)

I'm not sure where my previous answer came from but here is a very simple and straight forward example:

<?php

$foo = array("bar", "baz");

print_r($foo);

array_shift($foo);

print_r($foo);

?>

Output is as follows:

Array
(
    [0] => bar
    [1] => baz
)

Array
(
    [0] => baz
)

If you run array_shift one more time, output is as follows:

Array
(
)

And once more:

Array
(
)

Given this, it seems the conditions you have are unnecessary.


//Try This one

<?php
//array
$data=array("0" => 0,"1" => 30,"2" => 14);
//print array without apply array_shift function 
echo '<pre>';
print_r($data);
echo '</pre>';

if(end($data) != 0){
    if($data[0] == 0){
        array_shift($data);  
    }
}
//print array with apply array_shift function
echo '<pre>';
print_r($data);
echo '</pre>';
?>



//output
  Array
   (
    [0] => 0
    [1] => 30
    [2] => 14
   )

 Array
 (
   [0] => 30
   [1] => 14
 )


Your code seems straight forward, try to see if your conditions are resolving to true in the first place:

if(end($this->region_id) != 0){
    exit('I am first condition');

    if($this->region_id[0] == 0){
        exit('I am second condition');

        array_shift($this->region_id);  
    }
}

This way you will come to know whether or not you reach at array_shift.


Just thinking here, but can you try this?

array_shift(&$this->region_id);

The reasoning is that perhaps a copy of the array is returned instead of the actual array. If so: the operation is performed but not saved. Note that this is only the case with older PHP versions. Afaik from PHP5 on it will return a reference and will even complain about the reference operator.

Edit:

Can you just try this to eliminate the "it's a copy"-option?

$test = $this->region_id;
if(end($this->region_id) != 0){
  if($this->region_id[0] == 0){
    array_shift($test);  
  }
}

echo '<pre>';
print_r($test);
echo '</pre>';


$this->region_id was populated by $_POST['user']['region_id']; When i did this it worked

`if(end($this->region_id) != 0){ 
if($this->region_id[0] == 0){ 
array_shift($_POST['user']['region_id']); 
$this->region_id = $_POST['user']['region_id']; } 
} 

Althoug I still don't get it why the other method failed`

0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜