Traverse multi dimensional array recusively without using foreach

Posted by ejaz on Stack Overflow See other posts from Stack Overflow or by ejaz
Published on 2012-07-18T09:44:53Z Indexed on 2012/09/29 15:38 UTC
Read the original article Hit count: 174

I have an array like this and the code using foreach loop.

$arr = array( array ( array( 'CAR_TIR', 'Tires', 100 ),
           array( 'CAR_OIL', 'Oil', 10 ),
           array( 'CAR_SPK', 'Spark Plugs', 4 )
          ),
       array ( array( 'VAN_TIR', 'Tires', 120 ),
           array( 'VAN_OIL', 'Oil', 12 ),
           array( 'VAN_SPK', 'Spark Plugs', 5 )
          ),
       array ( array( 'TRK_TIR', 'Tires', 150 ),
           array( 'TRK_OIL', 'Oil', 15 ),
           array( 'TRK_SPK', 'Spark Plugs', 6 )
          )
      );


function recarray($array)
{
    foreach($array as $key=>$value)
    {
        if(is_array($value))
        {
            RecArray($value);
        }
        else
        {
            echo "key = $key value = $value";
       }
    }
}
recarray($arr);

I have to traverse the array using recursion and without using foreach. I would appreciate it if any one can help me

© Stack Overflow or respective owner

Related posts about php

Related posts about recursion