如何循环遍历具有相同值的数组并将它们分组

问题描述 投票:0回答:2

我有一个嵌套的 foreach 循环,可以迭代类和产品。在一种情况下,我的瑜伽课程有两种课程:第 1 节和第 2 节。 报名参加课程意味着参加所有日期。

第 1 节有 5 个唯一日期,第 2 节有 5 个唯一日期。目前,我的 foreach 循环将每个循环显示为它自己的“产品”: 瑜伽 第一节 2016年10月1日 下午 5 点至 6 点

第一节 2016年10月8日 下午 3 点至 4 点

第一节 2016年10月11日 上午 10 点至上午 11 点

第一节 2016年10月12日 上午 10 点至上午 11 点

第一节 2016年10月15日 下午 4 点至下午 5 点

第二场 16年11月1日 下午 5 点至 6 点

第二场 16年11月9日 下午 3 点至 4 点

第二场 16年11月15日 上午 10 点至上午 11 点

第二场 2016年11月16日 上午 10 点至上午 11 点

第二场 16年11月23日 下午 4 点至 5 点

所以现在我遇到了问题。也许我需要另一个循环,以便在每个会话产品中列出 5 个唯一日期?

第一节

16年10月1日 下午 5 点至 6 点

10/8/16 下午 3 点至 4 点

2016年10月11日 上午 10 点至上午 11 点

2016年10月12日 上午 10 点至上午 11 点

10/15/16 下午 4 点至 5 点

第二场

16年11月1日 下午 5 点至 6 点

16年11月9日 下午 3 点至 4 点

16年11月15日 上午 10 点至上午 11 点

2016年11月16日 上午 10 点至上午 11 点

16年11月23日 下午 4 点至 5 点

foreach ($rows as $row) {

if ($row['offering']=="Session 1"){
// do something

}

if ($row['offering']=="Session 2") {
// do something
}

}

尝试过此操作,但它仍然显示所有内容为单独的行

$array = array($row['offering']);
    foreach ($array as $key => $val) {
   // do stuff
echo $val;
}

}

我没有走上正确的道路...希望得到一些指导。

示例 var_dump

["YOGA"]=> array(10) { 
[0]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "0550AA8D-F437-4578-BD98-95C51D017E62" ["offering"]=> string(15) "YOGA Session 1" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "We" ["StartDate"]=> string(10) "10/05/2016" ["EndDate"]=> string(10) "10/05/2016" ["StartTime"]=> string(7) " 6:00PM" ["EndTime"]=> string(7) "10:00PM" ["Location"]=> string(23) "Off-Site, Quiet Room" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(1) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Sep 29 2016 12:00AM" } 

[1]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "0550AA8D-F437-4578-BD98-95C51D017E62" ["offering"]=> string(15) "YOGA Session 1" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Su" ["StartDate"]=> string(10) "10/09/2016" ["EndDate"]=> string(10) "10/09/2016" ["StartTime"]=> string(7) " 9:00AM" ["EndTime"]=> string(7) " 5:00PM" ["Location"]=> string(8) "Off-Site" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(1) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Sep 29 2016 12:00AM" } 

[2]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "0550AA8D-F437-4578-BD98-95C51D017E62" ["offering"]=> string(15) "YOGA Session 1" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "We" ["StartDate"]=> string(10) "10/12/2016" ["EndDate"]=> string(10) "10/12/2016" ["StartTime"]=> string(7) " 6:00PM" ["EndTime"]=> string(7) "10:00PM" ["Location"]=> string(13) "Quiet Room" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(1) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Sep 29 2016 12:00AM" } 

[3]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "0550AA8D-F437-4578-BD98-95C51D017E62" ["offering"]=> string(15) "YOGA Session 1" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Sa" ["StartDate"]=> string(10) "10/15/2016" ["EndDate"]=> string(10) "10/15/2016" ["StartTime"]=> string(7) " 9:00AM" ["EndTime"]=> string(7) " 5:00PM" ["Location"]=> string(11) "YOGA Class" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(1) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Sep 29 2016 12:00AM" } 

[4]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "0550AA8D-F437-4578-BD98-95C51D017E62" ["offering"]=> string(15) "YOGA Session 1" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Su" ["StartDate"]=> string(10) "10/16/2016" ["EndDate"]=> string(10) "10/16/2016" ["StartTime"]=> string(7) " 9:00AM" ["EndTime"]=> string(7) " 5:00PM" ["Location"]=> string(11) "YOGA Class" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(1) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Sep 29 2016 12:00AM" } 

[5]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "DE7A6045-2908-47B6-AD13-6DC12FF42F6E" ["offering"]=> string(15) "YOGA Session 2" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Tu" ["StartDate"]=> string(10) "11/01/2016" ["EndDate"]=> string(10) "11/01/2016" ["StartTime"]=> string(7) " 6:00PM" ["EndTime"]=> string(7) "10:00PM" ["Location"]=> string(23) "Off-Site, Quiet Room" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(3) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Oct 26 2016 12:00AM" } 

[6]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "DE7A6045-2908-47B6-AD13-6DC12FF42F6E" ["offering"]=> string(15) "YOGA Session 2" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Su" ["StartDate"]=> string(10) "11/06/2016" ["EndDate"]=> string(10) "11/06/2016" ["StartTime"]=> string(7) " 9:00AM" ["EndTime"]=> string(7) " 5:00PM" ["Location"]=> string(8) "Off-Site" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(3) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Oct 26 2016 12:00AM" } 

[7]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "DE7A6045-2908-47B6-AD13-6DC12FF42F6E" ["offering"]=> string(15) "YOGA Session 2" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Tu" ["StartDate"]=> string(10) "11/08/2016" ["EndDate"]=> string(10) "11/08/2016" ["StartTime"]=> string(7) " 6:00PM" ["EndTime"]=> string(7) "10:00PM" ["Location"]=> string(13) "Quiet Room" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(3) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Oct 26 2016 12:00AM" } 

[8]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "DE7A6045-2908-47B6-AD13-6DC12FF42F6E" ["offering"]=> string(15) "YOGA Session 2" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Sa" ["StartDate"]=> string(10) "11/12/2016" ["EndDate"]=> string(10) "11/12/2016" ["StartTime"]=> string(7) " 9:00AM" ["EndTime"]=> string(7) " 5:00PM" ["Location"]=> string(11) "YOGA Class" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(3) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Oct 26 2016 12:00AM" } 

[9]=> array(20) { ["category"]=> string(7) "Classes" ["course"]=> string(5) "YOGA" ["CourseOfferingID"]=> string(36) "DE7A6045-2908-47B6-AD13-6DC12FF42F6E" ["offering"]=> string(15) "YOGA Session 2" ["Semester"]=> string(9) "Fall 2016" ["Days"]=> string(2) "Su" ["StartDate"]=> string(10) "11/13/2016" ["EndDate"]=> string(10) "11/13/2016" ["StartTime"]=> string(7) " 9:00AM" ["EndTime"]=> string(7) " 5:00PM" ["Location"]=> string(11) "YOGA Class" ["pricing"]=> NULL ["SIZE"]=> int(10) ["REGISTERED_COUNT"]=> int(3) ["WAITING_LIST_COUNT"]=> int(0) ["ProgramClassificationID"]=> string(36) "AFCAB0B2-24FE-427B-8590-812D74749E6A" ["ProgramClassificationName"]=> string(8) "Wellness" ["SemesterID"]=> string(36) "854E61F8-B564-48F6-AFCD-FAEE597AFF0C" ["CourseID"]=> string(36) "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B" ["RegCloseDate"]=> string(19) "Oct 26 2016 12:00AM" } }
php arrays loops cakephp foreach
2个回答
0
投票

如果您确实想使用这种格式的数组,那么您可以通过以下代码获得此解决方案,但这不应该使用,因为它不是最佳解决方案。你必须为此使用正确的数据结构,尽管我已经给出了临时解决你的问题的代码:

$yoga = [["category" => "Classes","course" => "Yoga","CourseOfferingId" => "0550AA8D-F437-4578-BD98-95C51D017E62","offering" => "YOGA Session 1","Semester" => "Fall 2016","Days" =>"We", "StartDate" => "10/05/2016", "EndDate" => "10/05/2016", "StartTime" => " 6:00PM", "EndTime" => "10:00PM","Location" => "Off-Site, Quiet Room","pricing" => NULL,"SIZE" =>10,"REGISTERED_COUNT" => 10,"WAITING_LIST_COUNT" => 0,"ProgramClassificationID" => "AFCAB0B2-24FE-427B-8590-812D74749E6A","ProgramClassificationName" =>"Wellnes","SemesterID" => "854E61F8-B564-48F6-AFCD-FAEE597AFF0C","CourseID" => "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B","RegCloseDate" => "Sep 29 2016 12:00AM"],
     ["category" => "Classes","course" => "Yoga","CourseOfferingId" => "0550AA8D-F437-4578-BD98-95C51D017E62","offering" => "YOGA Session 2","Semester" => "Fall 2016","Days" =>"We", "StartDate" => "10/05/2011", "EndDate" => "10/05/2016", "StartTime" => " 6:00PM", "EndTime" => "10:00PM","Location" => "Off-Site, Quiet Room","pricing" => NULL,"SIZE" =>10,"REGISTERED_COUNT" => 10,"WAITING_LIST_COUNT" => 0,"ProgramClassificationID" => "AFCAB0B2-24FE-427B-8590-812D74749E6A","ProgramClassificationName" =>"Wellnes","SemesterID" => "854E61F8-B564-48F6-AFCD-FAEE597AFF0C","CourseID" => "84A92D24-12C3-4487-93C1-4DBFAF2D9A3B","RegCloseDate" => "Sep 29 2016 12:00AM"]];

$sessions = [];
$sessionInformation = [];
foreach($yoga as $key => $yogaElements)
{
   $temporarySessionInfo = [];
   $sessionName = "";
   foreach($yogaElements as $key => $value )
   {
        if($key == "offering"){
           $sessionName = explode(" ",$value)[1]." ".explode(" ",$value)[2];
        }
        if($key == "StartDate"){
           array_push($temporarySessionInfo,"Start Date: ".$value);
        }
        if($key == "EndDate"){
           array_push($temporarySessionInfo,"End Date: ".$value);
        }
        if($key == "StartTime"){
           array_push($temporarySessionInfo,"StartTime: ".$value);
        }
        if($key == "EndTime"){
           array_push($temporarySessionInfo,"End Time: ".$value);
        }   
   }
   if(!array_key_exists($sessionName,$sessions))
   {
       $sessions[$sessionName] = $temporarySessionInfo;
   }
   else
   {
    $sessions[$sessionName] = array_merge($sessions[$sessionName], $temporarySessionInfo);
   }
}

foreach($sessions as $key => $values)
{
   echo $key."<br/>";
   foreach($values as $value)
   {
      echo $value."<br/>";
   }
   echo "<br/>";
}

0
投票

我最终在嵌套的 foreach 中使用了它......

if ($key=="YOGA") 
{
for($i=0;$i<count($value);$i++)
{if(!array_key_exists($value[$i]["offering"], $class))     
$class[$value[$i]["offering"]] = array();
array_push($class[$value[$i]["offering"]], $value[$i]);
}

    foreach($class as $key => $rows):
    //echo something
    echo $key;
        $k = 0;
        foreach ($rows as $row) :
        //echo something
        $k++;
        endforeach;
    endforeach;
} else {
// show remaining non-YOGA classes
}
© www.soinside.com 2019 - 2024. All rights reserved.