Home » Php » php – How do I print an item from a multidemnsional array in Smarty and subsequently loop through it?

php – How do I print an item from a multidemnsional array in Smarty and subsequently loop through it?

Posted by: admin February 25, 2020 Leave a comment

Questions:

I am very new to Smarty and PHP in general and am quite struggling with multidimensional arrays. I have a multidimensional array called $tag_groups. When I print the array with {$tag_groups|@debug_print_var} I get the following output:

Array (2)
0 => Array (2)
  tags => Array (4)
    0 => Array (8)
      id => "10"
      name => "Os coxa"
      url_name => "axial_os_coxa"
      lastmodified => "2020-02-18 10:42:13"
      counter => 76
      level => 3
      group => "Axial"
      URL => "index.php?/tags/10-axial_os_coxa"
    1 => Array (8)
      id => "12"
      name => "Rib"
      url_name => "axial_rib"
      lastmodified => "2020-02-18 10:42:13"
      counter => 76
      level => 3
      group => "Axial"
      URL => "index.php?/tags/12-axial_rib"
    2 => Array (8)
      id => "13"
      name => "Sternum"
      url_name => "axial_sternum"
      lastmodified => "2020-02-18 10:42:13"
      counter => 76
      level => 3
      group => "Axial"
      URL => "index.php?/tags/13-axial_sternum"
    3 => Array (8)
      id => "11"
      name => "Vertebra"
      url_name => "axial_vertebra"
      lastmodified => "2020-02-18 10:42:13"
      counter => 76
      level => 3
      group => "Axial"
      URL => "index.php?/tags/11-axial_vertebra"
  TITLE => "Axial"
1 => Array (3)
  tags => Array (2)
    0 => Array (8)
      id => "17"
      name => "Pan"
      url_name => "extant_primates_pan"
      lastmodified => "2020-02-18 10:42:13"
      counter => 76
      level => 3
      group => "Extant primates"
      URL => "index.php?/tags/17-extant_primates_pan"
    1 => Array (8)
      id => "18"
      name => "Pongo"
      url_name => "extant_primates_pongo"
      lastmodified => "2020-02-18 10:42:13"
      counter => 76
      level => 3
      group => "Extant primates"
      URL => "index.php?/tags/18-extant_primates_pongo"
  CHANGE_COLUMN => true
  TITLE => "Extant primates"

Array (2) is $tag_groups and the next two lower levels (i.e. 0 => Array (2) and 1 => Array (3)) are $tag_group.

How do I print only the TITLE of each $tag_group, or at least a sublevel?

I tried

{$tag_groups.TITLE|@debug_print_var}
{$tag_groups['tag_group']|@debug_print_var}

And a few other variations and I either always get null or a php error (e.g. when I try {$tag_groups item='tag_group'|@debug_print_var}).

My thinking is that if I am able to print only the TITLE of each sublevel of the array I will then know what syntax to use in order to loop through it with an if condition, as what I want to do will look something like that

{foreach foreach from=$tag_groups item=tag_group.TITLE}
  {if in_array('Axial', $tag_group.TITLE)}
   DoSomethingAboutIt
  {/if}
{/foreach} 

But I am not even sure this is valid thinking.

Thanks for the help!

How to&Answers:

Ok, I think I worked it out

{foreach from=$tag_groups item=tag_group}
    {$tag_group.TITLE|@debug_print_var}
{/foreach}

I am coming from R so it just didn’t make sense for me to loop through the array in order to print its subelements….