4

I created a simple test case that replicates an issue I'm having.

I'm walking through a two dimensional array using the next() and current() functions, and am wanting to setup the array pointer to a specific location. So, given a 2D array with a variable name of $food with the following array structure:

array
  0 => <-- POINTER LOCATION
    array
      0 => string 'apple' <-- POINTER LOCATION
      1 => string 'orange'
  1 => 
    array
      0 => string 'onion'
      1 => string 'carrot'

... And the following code snippet:

// move the inner array's pointer once
$should_be_orange = next(current($food));

// now check that inner array's value
$should_still_be_orange = current(current($food));

... Why is the value of $should_be_orange "orange" but the value of $should_still_be_orange "apple"? Is this because the current() function returns a copy of the inner array, who's pointer gets iterated, and then destroyed (leaving the original array untouched)? Or am I simply doing something wrong that I'm not catching?

At the root of the question, how do you move the inner array's pointer given that you don't know the outer array's key (and must use the current() function to obtain the outer array's pointer location)?

2 Answers 2

2

Actually current() returns an element from the array. In your case, this element is also an array, and that's why next() is working at all in your code. Your next() do not work on the $food array, but on copy of $food[0], returned by current()

Sign up to request clarification or add additional context in comments.

1 Comment

Okay, exactly what I suspected. Assuming I always have a two dimensional array (like the example), do you have a suggestion on how I can move the pointer of the inner array?
1

You can't pass function in arguments, you can must only variables, because argument is reference:

function current(&$array) {...}
function next(&$array) {...}

so proper syntax is:

// move the inner array's pointer once
$tmp = current($food);
$should_be_orange = next($tmp);

// now check that inner array's value
$tmp = current($food);
$should_still_be_orange = current($tmp);
                 ^^^^^^ NO! It should be "apple" ! When you do next($tmp) it will be orange !

Demo: http://codepad.viper-7.com/YZfEAw

Documentation:


When you are learning PHP you should display ALL errors using command:

error_reporting(E_ALL);

Using this u should receive notice:

Strict Standards: Only variables should be passed by reference in (...) on line (...)

(i think this answer need review cause of english gramma)

4 Comments

The documentation is not in sync with the current implementation. You are supposed to pass variables to next() and current(), but expressions / array constants work too.
Yes it will work, but it will create Strict standards error as is mention above. Passing function instead of variable is main problem here.
In which version did you test that? Because it does neither in PHP 5.3 nor 5.4 for me. -- And no, the main problem is that no reference is returned, as OP already suspected.
Yes! All info is in documentation.

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.