如何使用foreach循环根据索引显示数组中的数据

时间:2015-03-11 11:14:49

标签: php arrays

我需要什么

  • 我需要使用foreach循环在php中根据索引追加数据。

php code

              foreach($data[$k]['agenda'] as $key=>$value)
                {
                    if($key >1)
                    {

                        print_r($data[$k]['agenda'][$key]);

                    }


                }

数组结构

                  Array
                (
                [title] => The First 10 Steps to Taking Your Retail Business Online
                [description] => 
                [Start_time] => 10:15 AM
                [end_time] => 11:45 AM
                [days] => 2014-02-05
                )
                Array
                (
                [title] => Tricks
                [description] => 
                [Start_time] => 11:45 AM
                [end_time] => 01:00 PM
                [days] => 2014-02-05
                )
                Array
                (
                [title] => Lunch and Networking
                [description] => 
                [Start_time] => 01:00 PM
                [end_time] => 02:00 PM
                [days] => 2014-02-05
                )
                Array
                (
                [title] => Launch of Online Sellers Association of India (OSAI)
                [description] => 
                [Start_time] => 06:15 PM
                [end_time] => 06:15 PM
                [days] => 2014-02-07
                )
  • 显示视图中的数据

       if($data[$k]['agenda'])
    
        {
    
        $content .='<p><span>Topic:</span><b>'.$data[$k]['agenda'][$key]['title'].'</b></p>';
        }
    

输出显示

主题:推出印度在线销售商协会(OSAI)

而不是o / p

如果它来自2个索引

主题:午餐和网络

依此从索引大于2。

1 个答案:

答案 0 :(得分:0)

您需要在循环中追加数据。现在,$key只有最后一个值。

$content = ''; // if $content doesn't exist yet, define 

foreach($data[$k]['agenda'] as $key => $value) {
    if($key > 1) { // do you need this condition?
        $content .= '<p><span>Topic:</span><b>' . $data[$k]['agenda'][$key]['title'] . '</b></p>';
    }
}