如何在foreach循环中将数组与公共值组合在一起

时间:2016-03-15 21:14:49

标签: php arrays

我不确定我是否正确地问这个问题,但是我想根据活动标题组织一系列课程。然而,活动标题(见底部的数组)是不同的,但确实有像陶瓷和陶瓷 - 中间体这样的共同属性 - 这些属于一个名为陶瓷的专栏。 期望的结果:(图像) organized

目前,我的foreach循环为每个课程吐出一列。

each in own column

<?php

$calendar = array();
foreach ($course as $row)
{
    $calendar[$row['CurrentCourse']['course_title']][] = $row;
}
?>
<div>
    <?php
    foreach($calendar as $key => $rows):
        ?>
        <div class="studio">
            <div class="column">
                <h2>
                    <?php
                    $mystudio = $key;
                    $flame = 'Flame';
                    $clay = 'Ceramics';
                    $metal = 'Jewelry';
                    $glass = 'Stained';
                    $wood = 'Wood';
                    if (strpos($mystudio, $flame) !== FALSE) {
                        echo "Flame Working";
                        $color = "pale-red";
                    }
                    elseif (strpos($mystudio, $clay) !== FALSE) {
                        echo "Ceramics";
                        $color = "pale-orange";
                    }
                    elseif (strpos($mystudio, $metal) !== FALSE) {
                        echo "Jewelry";
                        $color = "pale-yellow";
                    }
                    elseif (strpos($mystudio, $glass) !== FALSE) {
                        echo "Stained Glass";
                        $color = "pale-purple";
                    }
                    elseif (strpos($mystudio, $wood) !== FALSE) {
                        echo "Woodshop";
                        $color = "pale-tan";
                    }
                    else {
                    }
                    ?>
                </h2>
                <?php
                foreach($rows as $r):
                    ?>
                    <div class="time-slot <?php echo $color; ?>">
            <span class="title"><strong>
                    <?php echo $r["CurrentCourse"]["course_title"]; ?></strong></span>
            <span class="time">
                        <?php
                        $newST = strftime('%-I:%M %p', 
                            strtotime($r["CurrentCourse"]["course_start"]));
                        echo $newST;
                        ?>
                &nbsp;to&nbsp;
                <?php
                $newET = strftime('%-I:%M %p', 
                    strtotime($r["CurrentCourse"]["course_end"]));
                echo $newET;
                ?> 
                        </span>
                    </div><!-- end time slot -->
                <?php endforeach; ?>
            </div><!-- end div column -->
        </div><!-- end div studio -->
    <?php endforeach;?>
</div><!-- end div  -->

下面是var_dump($ row);

array(2) { [0]=> array(2) { ["day_index"]=> string(1) "3" ["time"]=> string(8) "10:00:00" } 
        ["CurrentCourse"]=> array(6) { ["course_start"]=> string(19) "2016-01-26 10:00:00" ["daysofweek"]=> string(4) "Tue " ["activity_title"]=> string(60) "Ceramics Class " ["course_title"]=> string(60) "Beginning Ceramics, Wheel " ["course_end"]=> string(19) "2016-03-01 12:00:00"["id"]=> string(4) "6577" } }
        array(2) { [0]=> array(2) { ["day_index"]=> string(1) "3" ["time"]=> string(8) "11:00:00" } 
        ["CurrentCourse"]=> array(6) { ["course_start"]=> string(19) "2016-01-26 11:00:00" ["daysofweek"]=> string(4) "Tue " ["activity_title"]=> string(60) "Jewelry Smithing Class " ["course_title"]=> string(60) "Jewelry Smithing " ["course_end"]=> string(19) "2016-03-01 13:00:00" ["id"]=> string(4) "6655" } } 
        array(2) { [0]=> array(2) { ["day_index"]=> string(1) "3" ["time"]=> string(8) "11:00:00" } 
        ["CurrentCourse"]=> array(6) { ["course_start"]=> string(19) "2016-01-26 11:00:00" ["daysofweek"]=> string(4) "Tue " ["activity_title"]=> string(60) "Jewelry Casting Class " ["course_title"]=> string(60) "Jewelry Casting " ["course_end"]=> string(19) "2016-03-01 13:00:00" ["id"]=> string(4) "6610" } } 
        array(2) { [0]=> array(2) { ["day_index"]=> string(1) "3" ["time"]=> string(8) "12:00:00" } 
        ["CurrentCourse"]=> array(6) { ["course_start"]=> string(19) "2016-01-26 12:00:00" ["daysofweek"]=> string(4) "Tue " ["activity_title"]=> string(60) "Flame Working " ["course_title"]=> string(60) "Beginning Flameworking " ["course_end"]=> string(19) "2016-03-01 14:00:00"  ["id"]=> string(4) "6560" } } 
        array(2) { [0]=> array(2) { ["day_index"]=> string(1) "3" ["time"]=> string(8) "15:30:00" } 
        ["CurrentCourse"]=> array(6) { ["course_start"]=> string(19) "2016-01-26 15:30:00" ["daysofweek"]=> string(4) "Tue " ["activity_title"]=> string(60) 
    "Flame Working Intermediate " ["course_title"]=> string(60) "Intermediate Flameworking " ["course_end"]=> string(19) "2016-03-01 17:30:00" ["id"]=> string(4) "6574" } } 
        array(2) { [0]=> array(2) { ["day_index"]=> string(1) "3" ["time"]=> string(8) "16:00:00" } 
        ["CurrentCourse"]=> array(6) { ["course_start"]=> string(19) "2016-04-12 16:00:00" ["daysofweek"]=> string(4) "Tue " ["activity_title"]=> string(60) "Ceramics-Intermediate " ["course_title"]=> string(60) "Intermediate Ceramics, Wheel " ["course_end"]=> string(19) "2016-05-17 18:00:00" ["id"]=> string(4) "6706" } }    

1 个答案:

答案 0 :(得分:0)

由于您无法修改源以包含类别,您可能需要首先动态地执行此操作,然后使用these函数之一对其进行排序。

这是一个快速草图:

$categories =
     [
         "flame" => "pale-red",
         "ceramics" => "pale-orange",
     ];

foreach ($course as $row)
{
    $courseTitle = $row['CurrentCourse']['course_title'];        

    foreach ($categories as $cat)
    {
        if (stristr(strtolower($courseTitle), $cat) !== FALSE)
            $row['category'] = $categories[$cat];
    }

    // If no category has found add code here
    // to assign generic "unknown" category

    $calendar[$courseTitle][] = $row;
}