从查询字符串中提取所有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
)
)
最合适的回答,由SO网友:mor7ifer 整理而成
$string
将包含以逗号分隔的ID列表:
$ids = array();
foreach( $array as $post ) {
if( !in_array( $post->ID, $ids ) ) {
$ids[] = $post->ID;
}
}
$string = implode( \', \', $ids );
注意:这基本上不使用wordpress,除了post对象。。。这实际上只是一个物体。