how to create a new column from php foreach

时间:2015-10-30 22:32:49

标签: php multidimensional-array foreach

I have a group of classes on monday and they take place in 5 different rooms. I'd like to present them in columns based on location using divs and sort by location, start. I get the data no problem (find all classes that run on monday) but it simply lists it in one column so my foreach arrays are incorrect. here's $data

$calendar = array();
foreach ($data as $row) {
    $calendar[$row[0]][] = $row;
}
foreach($calendar as $key => $row) {
       foreach($row as $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"
} } 
    }  

table of classes in studios

    <?php foreach ($recNew as $key => $classes) { ?>
    <div class="locations-wrapper">
        <div class="my-locations"><?php echo $location[$key]; ?></div>
        <?php foreach ($classes as $class) : ?>
        <!-- class info here -->
        <div class="class">
            <h4><?php echo $class['calendars']['title']; ?></h4>
        </div><!-- end div class -->
        <?php endforeach; ?>
    </div>
    <?php } ?>

This is what I get table of classes

Might need the locations to be the key for this to work.

1 个答案:

答案 0 :(得分:2)

您可以尝试使用html表格打印结果:

<?php 
$calendar = array();
foreach ($data as $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 as $key => $row):
?>
<td>

<table>
<tr><td><?php echo $key;?></td></tr>
<?php 
foreach($row as $r):
?>
<tr><td><?php echo $r["title"]; ?></td></tr>
<?php endforeach;?>
</table>
</td>
<?php endforeach;?>
</tr>
</table>