PHP函数从数组打印数据

时间:2014-10-01 05:05:54

标签: php arrays

我需要编写一个php函数,将下面数组中的数据打印为HTML无序列表,其中包含带有'标题的超链接。 item作为链接副本,'路径' item作为链接的href,以及'类中的项目'数组作为这些链接的类。

$mymenu = array(
   0 => array(
      'path' => 'all',
      'title' => 'All Content',
      'attributes' => array(
        'class' => array('first-item', 'menu-item'),
        ),
      ),
   1 => array(
      'path' => 'videos',
      'title' => 'Videos',
      'attributes' => array(
        'class' => array('item', 'menu-item'),
        ),
      ),
   2 => array(
      'path' => 'articles',
      'title' => 'Articles',
      'attributes' => array(
        'class' => array('last-item', 'menu-item'),
        ),
      ),
);

这就是我所拥有的,我对我认为的属性有困难:

foreach($mymenu as $item) {
echo '<a href=' . $item['path'] . 'class="' . $item['attributes'] . '" />' . $item['title'] . '</a> <br>';

}

1 个答案:

答案 0 :(得分:0)

这似乎可以解决问题:

foreach($mymenu as $item) {
echo '<a href="'.$item['path'].'"class="'.$item['attributes']['class'][0] . ' ' .$item['attributes']['class'][1].'" />' . $item['title'] . '</a> <br>';  
}