Is there any function which can return, for example, the 3rd element of an associative array without any loop? (and without copying the array)
I know that I can do a for/foreach loop together with a counter, but I'm just curious.
PS: I already found this example, but I was expecting something from php which would do something like getAssociativeDataByIndex($array, $index) for example.
EDIT: content of this post:
function getAtPos($tmpArray,$pos) { return array_splice($tmpArray,$pos-1,1); } $array = array('Alpha' => 1.1,'Bravo' => 2.2,'Charlie' => 3.3,'Delta' => 4.4,'Echo' =>5.5, 'Golf' => 6.6); $pos = 3; $return = getAtPos($array,$pos); var_dump($return); // 3.3
PS2: I also found this answer, but the ArrayIterator class and the seek method is not (well) documented, so I'm not if this create a copy of the array or not (if it does, I do not think it's interesting)
EDIT: content of this post:
$keys = array_keys($array); echo $array[$keys[$position]]; OR The ArrayIterator class can also do this for you: $iterator = new ArrayIterator($array); $iterator->seek($position);
example of array:
$a = array('jean' => 3, 'simon' => 2, 'alex' => 5, 'derek' => 4);
Example of use case: you have an external library which takes an array in parameter, process it and return you an array like index=>boolean (or index=>data)
e.g.: array(1=>false, 2=>false, 3=>true)...