Assuming I have an associative array which looks like this:
$arr = array(
'a' => array(
'b' => 'b',
'c' => 'c',
'd' => array(
'e' =>'e'
),
'f' => 'f',
'g' => 'g'
),
'h' => 'h',
'i' => array(
'j' => 'j',
'k' => 'k'
)
);
Now I want to access array elements using integer-index:
0 will return array of key 'a'
1 will return array of key 'b'
2 will return array of key 'c'
3 will return array of key 'd'
4 will return array of key 'e'
..
11 will return array of key 'k'
I have tried to accomplish this by recursion using the following code:
function getElement($arr, $id)
{
static $counter = 0;
foreach($arr as $key => $val){
$varNum = count($val);
$total = $counter + $varNum;
if($counter == $id){
return $val;
}
elseif($total > $id){
$counter++;
$res = getElement($val, $id);
return $res;
}
$counter++;
}
}
However from index 4 the function fails.
any suggestions?