2010-07-25 55 views
1

我有以下代码array_shift不删除数组的第一个元素

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

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

不知怎的,它不删除数组的第一个元素,因为我的成绩长得一模一样的代码与print_r的

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

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

代码确实达到了数组移位。

+1

我只是C/P代码的另一种方法,用一个简单的数组替换'$ this-> region_id',它适用于我。 PHP 5.3.2 – robertbasic 2010-07-25 08:54:12

+0

$ this-> region_id由$ _POST ['user'] ['region_id']填充; 当我这样做,它的工作 如果(完($这个 - > REGION_ID)!= 0){ \t \t \t如果($这个 - > REGION_ID [0] == 0){ \t \t \t \t array_shift ($ _POST [ '用户'] [ 'REGION_ID']); \t \t \t \t $ this-> region_id = $ _POST ['user'] ['region_id']; \t \t \t} \t \t} Althoug我仍然不明白为什么其他方法,从0,第一个失败 – Roland 2010-07-25 09:02:40

回答

1

傻我。 :)

我不知道在哪里我以前的答案的来源,但这里是一个非常简单和直接的例子:

< PHP

$ FOO =阵列(“酒吧”, “巴兹”);

print_r($ foo);

array_shift($ foo);

print_r($ foo); ?

>

输出如下:

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

Array 
(
    [0] => baz 
) 

如果您运行array_shift一个更多的时间,输出如下:

Array 
(
) 

而且一次:

Array 
(
) 

鉴于此,看起来你所拥有的条件是不必要的。

1

有什么不对这样做只是这个

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

     array_shift($this->region_id); 

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

加入的条件执行了换档只发生如果数组的最后一个值是不同的一个是0 ...你不能只删除那些:) – Blizz 2010-07-25 09:29:51

0

一想到这里,但你可以试试这个?

array_shift(& $ this-> region_id);

原因可能是数组的副本返回而不是实际的数组。如果是这样:操作已执行但未保存。请注意,这只适用于旧版PHP版本。来自PHP5的Afaik将会返回一个引用,甚至会抱怨引用操作符。

编辑:

可你只是试试这个,消除“这是一个副本” - 选项?

$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>'; 
+1

array_shift的参数已经通过引用传递。 http://www.php.net/manual/en/function.array-shift.php >混合array_shift(array&$ array) – dockeryZ 2010-07-25 08:58:15

+0

当然是,函数如何修改呢?我所指的也是实际数组本身,如在“$ this-> region_id”中,也许是原始数组的副本。我们不知道该类是否返回现有属性,或者这是通过__get还是其他。只是试图消除可能性:) – Blizz 2010-07-25 09:01:16

+0

你可能是对的......如果它已经返回一个副本它不会变成一个参考通过添加运算符(它仍然是早期和星期日:P)。无论如何,我认为你需要在这个方向寻找问题。我从来没有见过array_shift不起作用。 – Blizz 2010-07-25 09:09:20

0

你的代码看起来向前伸直,尝试看看,如果你的条件是解析为真摆在首位:

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

这样你会知道你是否没有达到在array_shift

0

$ this-> region_id由$ _POST ['user'] ['region_id']填充;当我这样做,它的工作

`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我仍然不明白为什么failed`

1

//尝试这一个

<?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 
) 
相关问题