如何从查询字符串中提取所有ID变量?

时间:2012-02-04 作者:m-torin

从查询字符串中提取所有ID并在逗号分隔的列表中显示它们的最佳php方法是什么?

我有一个功能,可以从数据库中获取特定帖子,并尝试将这些ID输入到新的wp\\U查询循环中。

Array (
    [0] => stdClass Object (
        [ID] => 162
        [post_author] => 1
        [post_date] => 2011-10-22 09:25:46
        [post_date_gmt] => 2011-10-22 14:25:46
        [post_content] =>
        [post_title] => Paper III
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] =>
        [post_name] => paper-iii
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2011-12-22 09:22:05
        [post_modified_gmt] => 2011-12-22 14:22:05
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http:/mysite.org/?post_type=album&p=162
        [menu_order] => 0
        [post_type] => album
        [post_mime_type] =>
        [comment_count] => 0
        [user_nicename] => matt
    )
    [1] => stdClass Object (
        [ID] => 166
        [post_author] => 2
        [post_date] => 2011-12-22 11:32:46
        [post_date_gmt] => 2011-12-22 16:32:46
        [post_content] =>
        [post_title] => mtayla\'s album
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => closed
        [post_password] =>
        [post_name] => mtaylas-album
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2011-12-26 20:23:00
        [post_modified_gmt] => 2011-12-27 01:23:00
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http:/mysite.org/?post_type=album&p=166
        [menu_order] => 0
        [post_type] => album
        [post_mime_type] =>
        [comment_count] => 0
        [user_nicename] => mtayla
    )
    [2] => stdClass Object (
        [ID] => 308
        [post_author] => 2
        [post_date] => 2012-01-31 15:04:29
        [post_date_gmt] => 2012-01-31 20:04:29
        [post_content] =>
        [post_title] => another album
        [post_excerpt] =>
        [post_status] => publish
        [comment_status] => open
        [ping_status] => open
        [post_password] =>
        [post_name] => another-album
        [to_ping] =>
        [pinged] =>
        [post_modified] => 2012-01-31 15:04:29
        [post_modified_gmt] => 2012-01-31 20:04:29
        [post_content_filtered] =>
        [post_parent] => 0
        [guid] => http:/mysite.org/?post_type=album&p=308
        [menu_order] => 0
        [post_type] => album
        [post_mime_type] =>
        [comment_count] => 0
        [user_nicename] => mtayla 
    )
)

2 个回复
最合适的回答,由SO网友:mor7ifer 整理而成

$string 将包含以逗号分隔的ID列表:

$ids = array();
foreach( $array as $post ) {
    if( !in_array( $post->ID, $ids ) ) {
        $ids[] = $post->ID;
    }
}

$string = implode( \', \', $ids );
注意:这基本上不使用wordpress,除了post对象。。。这实际上只是一个物体。

SO网友:Stephen Harris

这不是WordPress的问题,但你可以使用array_map 提取ID,但将其放入数组中implode 那个

$id_array = array_map(\'my_id_extraction\', $rows);
function my_id_extraction($item){
    return $item->ID;
}
$id_list = implode(\',\', $id_array)

结束

相关推荐

使用原生jQuery AJAX风格而不是使用admin-ajax.php有什么问题吗?

我在使用管理ajax时遇到问题。php创建一个插件。我在使用oop模块的插件中使用“add\\u action(\'wp\\u ajax\\u ajax\\u action\',\'ajax\\u action\\u stuff\')”时遇到问题。因此,我想使用jquery原生方式,如下所示:$.ajax({ url: \"test.html\", context: document.body, success: function(){ $(this).