Group Array Based On Value

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
btpoole
Forum Newbie
Posts: 7
Joined: Tue Mar 10, 2015 8:32 am

Group Array Based On Value

Post by btpoole »

First, I know this should be easier than I have made it. I looked an tried other pieces of code from the net but can't seem to get my problem worked out. I have an xml that has been parsed into an array with the following:

Code: Select all

foreach ($items as $load){
$contents[]= simplexml_load_file($load['guide']); //LOAD XML EACH CHANNEL
				$control=[];
				foreach ($contents as $content){
					$control=array(
				'start'=>$content->programme['start'],
				'title'=>$content->programme->title,
				'desc'=>$content->programme->desc,		
				);
}
}
So basically I have an array that list the start time, show title , and show description. Now I would like to parse that array grouping all show titles by start times so I end up with array something like where all shows for particular time fall into same array. Any help greatly appreciated

Code: Select all

Array
(
    [20] => Array
        (
            [0] => Array
                (
                    [start] => 20161221400
                    [title] => Show Title A
                )

            [1] => Array
                (
                    [start] => 20161221400
                    [title] => Show Title B
                )
    [21] => Array
        (
            [0] => Array
                (
                    [start] => 20161221800
                    [title] => Show Title A
                )

            [1] => Array
                (
                    [start] => 20161221800
                    [title] => Show Title B
                )
        )
User avatar
Celauran
Moderator
Posts: 6427
Joined: Tue Nov 09, 2010 2:39 pm
Location: Montreal, Canada

Re: Group Array Based On Value

Post by Celauran »

Couldn't you do it in one go using show times as the array key?
btpoole
Forum Newbie
Posts: 7
Joined: Tue Mar 10, 2015 8:32 am

Re: Group Array Based On Value

Post by btpoole »

I am sure it can be done, but I don't know enough about PHP. I am posting all my code with hopes somebody can point me right direction. I am looping thru an xml, parsing a url where I have xml files stored, loading the xml, parsing out the start, title and desc into a control array. I then loop thru the start element comparing its value to the control, it the same it writes it to array along with title and desc. Finally I create one array of the start, title and desc, which will be saved to php array file. I am trying to group all titles (along with desc) by start.

Code: Select all

foreach ($items as $load){
$contents[]= simplexml_load_file($load['guide']); //LOAD XML EACH CHANNEL
     foreach ($contents as $content){   //BUILD CONTROL ARRAY
                $control=array(
		'start'=>$content->programme['start'],
		'title'=>$content->programme->title,
		'desc'=>$content->programme->desc,
		);
      }
		$start[]=$content->programme['start'];
      foreach($start as $base){
		if($base==$control['start']){  //COMPARE TO CONTROL
		echo $base."      ".$control['title']."<br>";
		$start[]=$control['start'];
		$title[]=$control['title'];
		$desc[]=$control['title'];
	}
       foreach($start as $final){
		$guide=array(
		'start'=>$final['start'],
		'title'=>$final['title'],
	        'desc'=>$final['desc'],
		);					
	}
}


Post Reply