SitePoint Sponsor

User Tag List

Results 1 to 5 of 5
  1. #1
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    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.

    PHP Code:
            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

  2. #2
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    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:

    PHP Code:
                        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); 

  3. #3
    Always A Novice bronze trophy
    K. Wolfe's Avatar
    Join Date
    Nov 2003
    Location
    Columbus, OH
    Posts
    2,178
    Mentioned
    63 Post(s)
    Tagged
    2 Thread(s)
    Quote Originally Posted by slaterino View Post
    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:

    PHP Code:
                        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?

  4. #4
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by K. Wolfe View Post
    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

  5. #5
    SitePoint Zealot
    Join Date
    Jul 2008
    Posts
    190
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    Problem solved! If anyone wants to see the final code, here it is:

    PHP Code:
    $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; 


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •