1

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
)

The code does reach the array shift.

2
  • 1
    I just c/p your code, replacing $this->region_id with a simple array and it works for me. PHP 5.3.2 Commented Jul 25, 2010 at 8:54
  • $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 Commented Jul 25, 2010 at 9:02

6 Answers 6

1

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>';
Sign up to request clarification or add additional context in comments.

1 Comment

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 :)
1

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.

Comments

1

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

Comments

0

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.

Comments

0

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>';

3 Comments

the parameter for array_shift is already passed by reference. php.net/manual/en/function.array-shift.php >mixed array_shift ( array &$array )
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 :)
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.
0

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

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.