简体   繁体   中英

How to show data from array according to index using foreach loop

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM