Group PHP Values Into An Array

Hi,
I’m hoping this is something quite basic. I’m trying to group values from a MySQL query into an array. I have a Wordpress set-up but I’m presuming this is more of a PHP rather than a Wordpress concern. I just want to know basically what is the best way of doing this. Essentially I currently have the following data:

Artist Name #1 - Event Date #1
Artist Name #2 - Event Date #2
Artist Name #1 - Event Date #3
Artist Name #1 - Event Date #4

And I want to find a way that I can group the Artist Names so that the data would output like this:

Artist Name #1
Event Date #1
Event Date #3
Event Date #4

Artist Name #2
Event Date #2

This is the code I have at the moment. How would I integrate an array and an extra Foreach loop to get the data organised like this. I’m still learning Arrays at the moment and would appreciate any assistance with this.

		global $post;		
		$args = array( 'post_type' => 'event', 'orderby' => 'meta_value', 'meta_key' => 'event_date', 'meta_compare' => '>', 'meta_value' => $todaysDate, 'numberposts' => 6, 'order' => 'ASC' );
		$whatson = get_posts( $args );
		$count = 1;
        foreach( $whatson as $post ) :
			echo get_the_title(get_post_meta($post->ID, '2artist', true)) . " - ";
			echo get_post_meta($whatson_query->ID, 'event_date', true);
		endforeach;

Thanks!
Russ

Okay, I’ve created an array now within the current foreach loop. How can I now echo this so that the outputted values are grouped by the artist name?? Here’s the array:

					if (get_post_meta($post->ID, '2artist', true)) :
						$artist_name = get_the_title(get_post_meta($post->ID, '2artist', true));
					elseif (get_post_meta($post->ID, '2artist_non', true)) :
						$artist_name =  get_post_meta($post->ID, '2artist_non', true);
					endif;
					$event_date = date('D j M', strtotime(get_post_meta($post->ID, 'event_date', true)));
		
		$artists = array(); //define dates array.
		$artists[] = $artist_name;
		$artists[] = $event_date;
		print_r ($artists);

Just curious, are you pulling this information from a table via SQL statement first?

Hey K, it’s in a Wordpress installation. I’m using their get_posts function. If you have a look at the first post you’ll see how I’m pulling the data from the database.

Best,
Russ

Problem solved! If anyone wants to see the final code, here it is:

$todaysDate = date('Y/m/d');
global $post;
$args = array( 'post_type' => 'event', 'orderby' => 'meta_value', 'meta_key' => 'event_date', 'meta_compare' => '>', 'meta_value' => $todaysDate, 'numberposts' => 6, 'order' => 'ASC' );
$whatson = get_posts( $args );
$output_array = array();

foreach( $whatson as $post ) :

if (get_post_meta($post->ID, '2artist', true)) :
$artist_name = get_the_title(get_post_meta($post->ID, '2artist', true));
elseif (get_post_meta($post->ID, '2artist_non', true)) :
$artist_name = get_post_meta($post->ID, '2artist_non', true);
endif;
$event_date = date('D j M', strtotime(get_post_meta($post->ID, 'event_date', true)));

$output_array[$artist_name][] = $event_date;

endforeach;

foreach($output_array as $artists=>$events) :
echo "<h1>$artists</h1>";
foreach($events as $event) :
echo "<h2>$event</h2>";
endforeach;
endforeach;