slaterino Posted November 19, 2012 Share Posted November 19, 2012 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 ); 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 Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/ Share on other sites More sharing options...
Iluvatar+ Posted November 19, 2012 Share Posted November 19, 2012 I have never used wordpress but i am guessing get_posts is a query method, try print-out '$args' array using echo '<pre>'; print_r($args); echo '</pre>'; See how the data your querying is structured and you should be able to work it out from there Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393574 Share on other sites More sharing options...
slaterino Posted November 19, 2012 Author Share Posted November 19, 2012 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); Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393575 Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 $artists = array(); then for each of your artistname/date pairs $artists[$artistname][] = $date; Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393577 Share on other sites More sharing options...
deoiub Posted November 19, 2012 Share Posted November 19, 2012 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); You want to look into a 2 dimensional array. setting it, with code something like this: $output_array = array() $output_array[$artist_name][] = $event_date; then outputting it something like this: foreach($output_array as $artists=>$events){ echo "<h1>$artists</h1>"; foreach($events as $event){ echo "<h2>$event</h2>"; } } Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393578 Share on other sites More sharing options...
slaterino Posted November 19, 2012 Author Share Posted November 19, 2012 Hey, I've tried using this two-dimensional array but still getting the standard result, which is where the artist names are not grouped together. I've included the code below. I can't help but think that I would need to break out of the initial foreach loop to be able to do this. Do you know what I mean? Here's the code: $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 ); foreach( $whatson as $post ) : $artist_name = get_the_title(get_post_meta($post->ID, '2artist', true)); $event_date = date('D j M', strtotime(get_post_meta($post->ID, 'event_date', true))); $output_array = array(); $output_array[$artist_name][] = $event_date; foreach($output_array as $artists=>$events){ print_r($output_array); echo "<h1>$artists</h1>"; foreach($events as $event){ echo "<h2>$event</h2>"; } } endforeach; Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393580 Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 (edited) $output_array = array(); Put that line before the loop. You are resetting the array to empty each time. Print the output after the loop when you have finished populating the array Edited November 19, 2012 by Barand Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393582 Share on other sites More sharing options...
slaterino Posted November 19, 2012 Author Share Posted November 19, 2012 And just to make clear, this is the output I'm getting: Array ( [Artist Name #1] => Array ( [0] => Tue 20 Nov ) ) Array ( [Artist Name #2] => Array ( [0] => Wed 28 Nov ) ) Array ( [Artist Name #2] => Array ( [0] => Thu 20 Dec ) Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393584 Share on other sites More sharing options...
slaterino Posted November 19, 2012 Author Share Posted November 19, 2012 Hmm, I just tried that Barand so that my code looks like the below, but my array only includes one entry, the last entry. Is there something I'm missing? $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 = array(); $output_array[$artist_name][] = $event_date; endforeach; print_r($output_array); And that's just outputting: Array ( [Artist Name #2] => Array ( [0] => Thu 20 Dec ) ) Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393586 Share on other sites More sharing options...
slaterino Posted November 19, 2012 Author Share Posted November 19, 2012 Guys. I got it! Sorry, I forgot to take out the $output_array from within the loop! I think I can work from what I have now. It looks like it works a charm. Thanks for your help! Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393587 Share on other sites More sharing options...
slaterino Posted November 19, 2012 Author Share Posted November 19, 2012 And in case 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; Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393588 Share on other sites More sharing options...
slaterino Posted November 19, 2012 Author Share Posted November 19, 2012 Hey all, sorry to come back onto this topic, but I have one final question. Essentially I need my data to be sorted by two different valuables, the date and the artist name. When I do a Foreach loop such as the one in the example above is there any way of sorting the $artist_name field alphabetically? Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393608 Share on other sites More sharing options...
Barand Posted November 19, 2012 Share Posted November 19, 2012 ksort Quote Link to comment https://forums.phpfreaks.com/topic/270908-group-php-values-into-an-array/#findComment-1393617 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.