Sunday, 24 January 2016

XML : PHP grouping array

I know this is has been asked and I have tried to follow what I have found on other post but for some reason I can't get it to work. I am parsing multiple xml files that contain tv programming. I am trying to group all shows by times. This is what I have.

    foreach ($items as $load){     $contents[]= simplexml_load_file($load['guide']); //LOAD XML EACH CHANNEL INTO ARRAY     }    echo "COUNT OF CONTENT. . . ".count($contents)."<br>";  //SHOULD BE 36 TIME SLOTS    foreach($contents as $content=> $b){ //BUILD MAIN ARRAYs              $start[]=$content->programme['start'];              $title[]=$content->programme->title;              $desc[]=$content->programme->desc;      }                 $i=0;              foreach($start[$i] as $a=>$b){                       if($b==$start[$i]){                                      $garray=array(                                      'start'=> $start[$i],                                      'title'=> $title[$i],                                      'desc'=> $desc[$i],                                      );                                      $i++;                                    }else{                                  $i++;                          }                                       }       

I anticipate a total of 36 values in $contents. I will loop thru each $contents creating an array for start, title and desc. Then I want to loop thru each $start to group all $title and $desc under it's corresponding $start. For some reason I can't make this work with what I have. If I run it as shown and check the count of $garray I have 3. I know for example the first $start I encounter should have 18 titles under the one $start. I would like to have it in the following format based on the start, each array would have one start each but multiple titles and descs. I anticipate saving the final $garray to an array file to be accessed by other programs. It could be saved to xml if need be. Any help greatly appreciated.

    array1(        0=> 'start', 'title 1', 'desc 1',        1=> 'start','title 2', 'desc 2',        2=> 'start', 'title 3', 'desc 3',        );    

No comments:

Post a Comment