Search code examples
phparraysloopscakephpforeach

How can I loop through array with one same value and group them


I have a nested foreach loop that iterates through classes and offerings. In one case, I have a Yoga class with two offerings; Session 1 and Session 2. Signing up for a session means attending all dates.

Session 1 has 5 unique dates and Session 2 has 5 unique dates. Currently, my foreach loop shows each as it's own "offering": Yoga Session 1 10/1/16 5pm - 6pm

Session 1 10/8/16 3pm - 4pm

Session 1 10/11/16 10am - 11am

Session 1 10/12/16 10am - 11am

Session 1 10/15/16 4pm - 5p

Session 2 11/1/16 5pm - 6pm

Session 2 11/9/16 3pm - 4pm

Session 2 11/15/16 10am - 11am

Session 2 11/16/16 10am - 11am

Session 2 11/23/16 4pm - 5pm

so now I'm running into a problem. Perhaps I need another loop so the 5 unique dates get listed within each Session offering?

Session 1

10/1/16 5pm - 6pm

10/8/16 3pm - 4pm

10/11/16 10am - 11am

10/12/16 10am - 11am

10/15/16 4pm - 5pm

Session 2

11/1/16 5pm - 6pm

11/9/16 3pm - 4pm

11/15/16 10am - 11am

11/16/16 10am - 11am

11/23/16 4pm - 5pm

foreach ($rows as $row) {

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

}

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

}

tried this but it still shows all as individual rows

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

}

I'm not on the right track...would appreciate some guidance.

A sample 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" } }

Solution

  • I ended up using this within my nested 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
    }