Hello. I am trying to create an array in Wordpress, create another array using the data from the first one, combine the two arrays and then parse and display the info.

Here is my code:

$wp_channels = $wpdb->get_results("SELECT *
							   FROM wp_site_posts 
							   JOIN wp_blogs ON wp_site_posts.blog_id = wp_blogs.blog_id
							   WHERE wp_site_posts.post_type = 'channel'
							   AND wp_site_posts.blog_public = 1	
							   AND wp_site_posts.blog_id != 1							   
LIMIT 0,2 "); $channels_arr = array($wp_channels); foreach ($wp_channels as $wp_channel) { switch_to_blog( $wp_channel->blog_id); $channel_id = get_post_meta($wp_channel->post_id, 'channel_id', true); $views[] = $wpdb->get_row(" SELECT `show_total_views` FROM `show` WHERE show_id = $channel_id "); restore_current_blog(); $views_arr = array($views); } $combined_channels = array_merge($channels_arr, $views);

Here is the output of the combined array using a print_r($combined_channels)

Array
(
[0] => Array
(
[0] => stdClass Object
(
[site_post_id] => 1446
[blog_id] => 11
[site_id] => 1
[sort_terms] => |blog_lang_en_en|all|
[blog_public] => 1
[post_id] => 40
[post_author] => 11
[post_title] => QA Demo Channel 4
[post_content] =>
[post_content_stripped] =>
[post_terms] => |12|
[post_permalink] => http://bcast.wp.testarea.glovue.com/channel/qa-demo-channel-4/
[post_published_gmt] => 2012-07-20 12:43:53
[post_published_stamp] => 1342788233
[post_modified_gmt] => 2012-07-20 12:43:53
[post_modified_stamp] => 1342788233
[post_type] => channel
[domain] => bcast.wp.testarea.glovue.com
[path] => /
[registered] => 2012-07-05 10:25:17
[last_updated] => 2012-11-16 17:56:51
[public] => 1
[archived] => 0
[mature] => 0
[spam] => 0
[deleted] => 0
[lang_id] => 0
)

        [1] => stdClass Object
            (
                [site_post_id] => 1256
                [blog_id] => 114
                [site_id] => 1
                [sort_terms] => |blog_lang_en_en|all|
                [blog_public] => 1
                [post_id] => 16
                [post_author] => 211
                [post_title] => ME2U
                [post_content] => see what the world is sharing
                [post_content_stripped] => see what the world is sharing
                [post_terms] => |
                [post_permalink] => [url]http://SharingSHIFT.wp.testarea.glovue.com/channel/me2u/[/url]
                [post_published_gmt] => 2012-02-27 22:47:53
                [post_published_stamp] => 1330382873
                [post_modified_gmt] => 2012-02-27 22:47:53
                [post_modified_stamp] => 1330382873
                [post_type] => channel
                [domain] => sharingshift.wp.testarea.glovue.com
                [path] => /
                [registered] => 2012-08-18 02:32:49
                [last_updated] => 2012-08-18 02:33:00
                [public] => 1
                [archived] => 0
                [mature] => 0
                [spam] => 0
                [deleted] => 0
                [lang_id] => 0
            )

    )

[1] => stdClass Object
    (
        [show_total_views] => 1
    )

[2] => stdClass Object
    (
        [show_total_views] => 14
    )

)

When trying to use a foreach loop to echo output, the "show_total_views works, but rest fails:

$i=0;
foreach ($combined_channels as $view) {


     // This line works correctly when used by itself
    echo $view->show_total_views;	

     // This line prints the first value correctly and then breaks
    echo $view[$i]->blog_id;


$i++;

}	

Can anybody help me in parsing this array or recreating it?

Thank you so much.

    Anytime you're executing one SQL query inside of a loop that's processing the results of another (related) SQL query, that's almost a sure sign of inefficient queries.

    Why not simply add a JOIN in the first query to get the data from the show table that you need?

      When iterating through a foreach loop and you want to access the key, you need to do a little more, like this:

      foreach($combined as $key => $view)
      {
          //Your code
      }
      

      Now you can do things like

      echo $view[$key]->blog_id;

      If the keys are numeric, the loop automatically increments them; you don't have to keep track of the key with a counter or increment it manually.

        Bonesnap;11018267 wrote:

        Now you can do things like

        echo $view[$key]->blog_id;

        No, not really... unless you had some very strangely-constructed multi-dimensional array on your hands. Maybe you meant just $view, or perhaps the redundant $combined[$key]?

          bradgrafelman;11018269 wrote:

          No, not really... unless you had some very strangely-constructed multi-dimensional array on your hands. Maybe you meant just $view, or perhaps the redundant $combined[$key]?

          It is a multidimensional array. $wpdb->get_results returns an array of standard class objects, which the OP then puts into another array, thus making it a multidimensional array.

            Bonesnap;11018267 wrote:

            When iterating through a foreach loop and you want to access the key, you need to do a little more, like this:

            foreach($combined as $key => $view)
            {
                //Your code
            }
            

            Now you can do things like

            echo $view[$key]->blog_id;

            Thanks for your replies.

            I tried this, but it didn't fix it. It is still doing the same thing.

              Write a Reply...