Output current 'numerical' value of array

Discussion in 'PHP' started by Omzy, Dec 1, 2008.

  1. #1
    I have an array as such:

    
    $items = array(
    	'1'=> array('1'=>'Menu 1 Item 1', '2'=>'Menu 1 Item 2', '3'=>'Menu 1 Item 3'),
    	'2'=> array('1'=>'Menu 2 Item 1', '2'=>'Menu 2 Item 2', '3'=>'Menu 2 Item 3')
    );
    Code (markup):
    I have a FOR loop that processes this array and prints out the text for each element contained in the two sub-arrays. But now I also need to get the 'number' of the element that is currently being processed too - how can this be done?
     
    Omzy, Dec 1, 2008 IP
  2. BrandonGregory

    BrandonGregory Peon

    Messages:
    28
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    0
    #2
    Can't you just use the loop variables to determine this? i.e. - echo (i+1);
     
    BrandonGregory, Dec 1, 2008 IP
  3. ariefsyu

    ariefsyu Active Member

    Messages:
    192
    Likes Received:
    2
    Best Answers:
    0
    Trophy Points:
    53
    #3
    this may solve your problem.
    
    foreach ($items as $key1=>$item)
    {
       echo "Array #$key1 :<br/>";
       foreach($item as $key2=>$detail_item)
       {
          echo "  $key2. $detail_item<br/>";
       }
    }
    
    
    Code (markup):
    arief
     
    ariefsyu, Dec 1, 2008 IP