foreach循环无法处理数组-请提供帮助


Trouble getting array to be processed by foreach loop - help please?

我正在构建一个目录站点,我一直在让foreach循环浏览多站点的博客ID。从代码中的注释中可以看出,对博客id的查询运行良好,print_r检查显示数组已填充,但当函数到达第一个foreach时,循环每次都返回相同的结果,foreach循环中site_blog_id上的print_r显示它为空。在循环中手动设置site_blog_id可以使其余代码正常工作,因此它肯定与foreach对数组的处理有关。

我非常困惑,因为这似乎与我在开发人员网站上看到的许多数组foreach代码示例相同,包括查询文档页面上的示例。我想知道是否需要对保存数组的site_blog_ids变量做些什么,使其与foreach一起工作,但坦率地说,我被卡住了。任何帮助都将不胜感激!David

/* get all subsite blog ids */
global $wpdb; 
$site_blog_ids = $wpdb->get_results($wpdb->prepare("SELECT blog_id FROM wp_blogs where blog_id > 1")); 
print_r( $site_blog_ids );
/* check output - shows "Array ( [0] => stdClass Object ( [blog_id] => 2 ) [1] => stdClass Object ( [blog_id] => 3 ) [2] => stdClass Object ( [blog_id] => 5 ) ) ". Looks fine? */
/* loop to iterate through the ids and display info from each blog */
foreach( $site_blog_ids AS $site_blog_id ) { 
print_r( "siteid= ".$site_blog_id."</br>" ); 
/* check output - this shows $site_blog_id is empty, no value. That's not right, should be each blog ID in turn from the site_blog_ids array. */
$oldblog = $wpdb->set_blog_id( $site_blog_id ); 
/* sets the system to use the loop blog ID instead of the current one. Since site_blog_id is empty, this doesn't work so the rest of the code uses the current blog, not the intended one. */
global $post;
$first_posts = get_posts('p=1'); //get the first post
foreach ( $first_posts AS $post ) {
    setup_postdata();
    the_title();
    the_excerpt();
    the_content();
    the_category();
}
}
/* Tell the $wpdb object to go back to using the current site */
$wpdb->set_blog_id( $oldblog );

尝试以下操作:

/* loop to iterate through the ids and display info from each blog */
foreach( $site_blog_ids AS $site_blog_id ) { 
print_r( "siteid= ". $site_blog_id->blog_id ."</br>"); 
/* check output - this shows $site_blog_id is empty, no value. That's not right, should be each blog ID in turn from the site_blog_ids array. */

print_r( "siteid= ".$site_blog_id."</br>" );不正确。

当它到达printr部分时,它将把它看作一个字符串。

我想你想做的是:

echo "siteid=". print_r($site_blog_id, true) ."</br>";