Expert Refresh

Pointer

1) How do you get current key from an array?




2) Advance the pointer in the array






key, current, next, reset <?php $arr = array('a' => 1, 'b' => 2, 'c' => 3); reset($arr); while (key($arr) !== NULL) { echo key($arr) . current($arr) . " "; next($arr); } // Output: a1 b2 c3 end, prev <?php // You could start your iteration from the last element $arr = array('a' => 1, 'b' => 2, 'c' => 3); end($arr); while (key($arr) !== NULL) { echo key($arr) . current($arr) . " "; prev($arr); } // Output: c3 b2 a1


References