0

I am having the following array format:

Array
(
    [0] => Array
       (
            [title] => Main Cat
            [subfield] => Array
              (
                [1] => Array
                    (                            
                        [sub_title] => Sub Cat 1
                    )

                [2] => Array
                    (                            
                        [sub_title] => Sub cat 2
                    )

               )
         )
       [1] => Array
        (
            [title] => Main cat 2
            [subfield] => Array
              (
                [1] => Array
                    (                            
                        [sub_title] => Test Cat 1                            
                    )

                [2] => Array
                    (                            
                        [sub_title] => Test Cat 2
                    )

               )
         )

I need to display it in the following format using foreach using PHP. O/P:

Main Cat

 Sub Cat 1

 Sub Cat 2

Main cat 2

 Test Cat 1

 Test Cat 2

How can this be done... Thanks in advance...

EDIT : My Try is

 $f = 1;
      foreach($vars['fields'] as $value) {
         $vars['topic_main'][] = $value['title']."<br>&nbsp;".$value['subfield'][$f]['sub_title']."<br>";
         $f++;
      }

      $vars['title'] = implode(' ' ,$vars['topic_main']);

My O/P is

Main Cat

 Sub Cat 1 


Main cat 2

 Test Cat 1
Fero
  • 12,969
  • 46
  • 116
  • 157
  • @Lukas: Kindly find the edited question – Fero Jul 03 '11 at 12:18
  • possible duplicate of [how do i get the values from array of array?](http://stackoverflow.com/questions/6557299/how-do-i-get-the-values-from-array-of-array) - This sort of question has been asked for numerous times already, so it's only one of the many duplicates. – hakre Jul 03 '11 at 12:18

3 Answers3

5

Assuming your array is called categories. (Edit: $vars['fields'] )

foreach($vars['fields'] as $cat){

    echo $cat['title'].'<br/>';
    foreach($cat['subfield'] as $sub){
        echo $sub['subtitle'].'<br/>';
    }
}
Eddie
  • 12,898
  • 3
  • 25
  • 32
  • is it possible to get the $cat['title'] and $sub['subtitle'] in a single array.... – Fero Jul 03 '11 at 12:30
  • There are many ways, the quickest would probably be to instead of echo the data out in this loop, simply add it to another array. – Eddie Jul 03 '11 at 12:33
1
echo '<ul>';
foreach ($array as $item):
    echo '<li>';
    echo $item['title'];
    echo '<ul>';
    foreach ($item['subfield'] as $sub):
        echo '<li>';
        echo $sub['subtitle'];
        echo '</li>';
    endforeach;
    echo '</ul>';
    echo '</li>';
endforeach;
echo '</ul>';
Tak
  • 11,428
  • 5
  • 29
  • 48
0

just use a for each and another foreach inside, what's the problem?

foreach ($arr as $main)
{  
  echo $main['title'];
  foreach ($main['subfield'] as $sub)
  {
     echo $sub['sub_title'];
   }

and do some cosmetics...

Chen Kinnrot
  • 20,609
  • 17
  • 79
  • 141