I am using recursive function to walk through array elements.But failed to get correct counter.What i did.
$i = 0 ;
$this->make_items($items,$i);
public function make_items($items,$i)
{
foreach($items as $item){
$i++;
echo '<position>'.$i.'</position>';
if(count($item['children'])){
$this->make_items($item['children'],$i);
}
}
But for array structure like
arr1
arr2
arr3
arr4
arr5
arr6
Output of my code is
<postion>1</position>
<postion>2</position>
<postion>3</position>
<postion>4</position>
<postion>4</position>
<postion>3</position>
While i needed
<postion>1</position>
<postion>2</position>
<postion>3</position>
<postion>4</position>
<postion>5</position>
<postion>6</position>
What i am doing wrong or what i understood wrong about recursion. If there is better way to do this then please advice. thanks in advance.