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

what i need

  • i need to append data according index in php using foreach loop.

php code

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

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

                    }


                }

array structure

                  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
                )
  • show the data in view

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

output is shown

topic: Launch of Online Sellers Association of India (OSAI)

instead of o/p should

if its from 2 index

topic : Lunch and Networking

and so on from index greater then 2.

You need to append data inside a loop. Now, $key has only the last value.

$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>';
    }
}