Jump to content

Group Php Values Into An Array


slaterino

Recommended Posts

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

Link to comment
Share on other sites

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);

Link to comment
Share on other sites

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>";

}

}

Link to comment
Share on other sites

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;

Link to comment
Share on other sites

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 ) )

Link to comment
Share on other sites

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; 

Link to comment
Share on other sites

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?

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.