tags:

views:

69

answers:

5

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

    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
)

Am i doning something wrong? And the code does reach the array shift.

+1  A: 

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.

Fake Code Monkey Rashid
A: 

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>';
Zane Edward Dockery
The conditions are added to enforce that the shift only happens if the last value of the array is different from 0 and the first one is 0... You can't just drop those :)
Blizz
A: 

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>';
Blizz
Zane Edward Dockery
Of course it is, how else could the function modify it? What I am referring too is that actual array itself, as in the "$this->region_id", perhaps is a copy of the original one. We don't know if the class returns an existing property or that this is via __get or something. Just trying to eliminate possibilities :)
Blizz
You are probably right... if it already returns a copy it's not gonna turn into a reference by adding the operator (it's still early and sunday :P). Anyway, I think you need to look for the problem in that direction. I've never seen array_shift not work.
Blizz
A: 

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.

Sarfraz
A: 

$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`

Roland