multidimensional array - how to create a new column from php foreach -
i have group of classes on monday , take place in 5 different rooms. i'd present them in columns based on location using divs , sort location, start. data no problem (find classes run on monday) lists in 1 column foreach arrays incorrect. here's $data
$calendar = array(); foreach ($data $row) { $calendar[$row[0]][] = $row; } foreach($calendar $key => $row) { foreach($row $field => $value) { $recnew[$field][] = $value; } }
here's array exerpt
array(1) { [""]=> array(20) { [0]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "742" ["title"]=> string(4) "yoga" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "20:00:00" ["end_time"]=> string(8) "21:00:00" ["location"]=> string(8) "studio 1" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [1]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "743" ["title"]=> string(2) "gx" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "18:00:00" ["end_time"]=> string(8) "18:45:00" ["location"]=> string(8) "studio 1" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [2]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "447" ["title"]=> string(4) "spin" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "09:15:00" ["end_time"]=> string(8) "10:00:00" ["location"]=> string(8) "studio 1" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [3]=> array(1) { ["calendars"]=> array(22) { ["id"]=> string(3) "448" ["title"]=> string(4) "spin" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "12:15:00" ["end_time"]=> string(8) "13:00:00" ["location"]=> string(8) "studio 1" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [4]=> array(1) { ["calendars"]=> array(22) { ["id"]=> string(3) "684" ["title"]=> string(4) "spin" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "16:30:00" ["end_time"]=> string(8) "17:15:00" ["location"]=> string(8) "studio 1" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [5]=> array(1) { ["calendars"]=> array(22) { ["id"]=> string(3) "545" ["title"]=> string(4) "yoga" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "18:15:00" ["end_time"]=> string(8) "19:00:00" ["location"]=> string(8) "studio 1" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [6]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "549" ["title"]=> string(4) "yoga" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "09:00:00" ["end_time"]=> string(8) "10:00:00" ["location"]=> string(8) "studio 2" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [7]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "688" ["title"]=> string(7) "weights" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "10:10:00" ["end_time"]=> string(8) "11:00:00" ["location"]=> string(8) "studio 2" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [8]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "572" ["title"]=> string(4) "yoga" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "12:10:00" ["end_time"]=> string(8) "13:10:00" ["location"]=> string(8) "studio 2" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [9]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "548" ["title"]=> string(4) "spin" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "16:00:00" ["end_time"]=> string(8) "16:55:00" ["location"]=> string(8) "studio 2" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [10]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "573" ["title"]=> string(11) "power yoga" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "18:45:00" ["end_time"]=> string(8) "19:45:00" ["location"]=> string(8) "studio 2" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [11]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "742" ["title"]=> string(4) "yoga" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "20:00:00" ["end_time"]=> string(8) "21:00:00" ["location"]=> string(8) "studio 2" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [12]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "552" ["title"]=> string(5) "zumba" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "10:15:00" ["end_time"]=> string(8) "11:15:00" ["location"]=> string(8) "studio 3" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [13]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "622" ["title"]=> string(4) "yoga" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "11:30:00" ["end_time"]=> string(8) "12:00:00" ["location"]=> string(8) "studio 3" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [14]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "570" ["title"]=> string(22) "body pump (doriana p.)" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "12:15:00" ["end_time"]=> string(8) "13:15:00" ["location"]=> string(8) "studio 3" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [15]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "740" ["title"]=> string(4) "spin" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "13:30:00" ["end_time"]=> string(8) "14:00:00" ["location"]=> string(8) "studio 4" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1 } } [16]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "623" ["title"]=> string(7) "weights" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "16:45:00" ["end_time"]=> string(8) "17:45:00" ["location"]=> string(8) "studio 4" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [17]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "586" ["title"]=> string(2) "gx" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "18:00:00" ["end_time"]=> string(8) "18:30:00" ["location"]=> string(8) "studio 5" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [18]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "613" ["title"]=> string(7) "weights" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "18:45:00" ["end_time"]=> string(8) "19:30:00" ["location"]=> string(8) "studio 5" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } [19]=> array(1) { ["calendars"]=> array(9) { ["id"]=> string(3) "621" ["title"]=> string(5) "zumba" ["start_date"]=> string(10) "2015-09-14" ["end_date"]=> string(10) "0000-00-00" ["start_time"]=> string(8) "19:45:00" ["end_time"]=> string(8) "20:45:00" ["location"]=> string(8) "studio 5" ["calendar_category_id"]=> string(1) "3" ["recur_mon"]=> string(1) "1" } } }
<?php foreach ($recnew $key => $classes) { ?> <div class="locations-wrapper"> <div class="my-locations"><?php echo $location[$key]; ?></div> <?php foreach ($classes $class) : ?> <!-- class info here --> <div class="class"> <h4><?php echo $class['calendars']['title']; ?></h4> </div><!-- end div class --> <?php endforeach; ?> </div> <?php } ?>
might need locations key work.
you can try using html table printing result:
<?php $calendar = array(); foreach ($data $row) { for($i=0;$i<count($row);$i++) { if(!array_key_exists($row[$i]["calendars"]["location"], $calendar)) $calendar[$row[$i]["calendars"]["location"]] = array(); array_push($calendar[$row[$i]["calendars"]["location"]], $row[$i]["calendars"]); } } ?> <table> <tr> <?php foreach($calendar $key => $row): ?> <td> <table> <tr><td><?php echo $key;?></td></tr> <?php foreach($row $r): ?> <tr><td><?php echo $r["title"]; ?></td></tr> <?php endforeach;?> </table> </td> <?php endforeach;?> </tr> </table>
Comments
Post a Comment