从多维数组中删除多个值 [英] Delete multiple values from multidimensional array

查看:47
本文介绍了从多维数组中删除多个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个WordPress的 get_posts()函数数组.

I have an array from WordPress' get_posts() function.

$ posts

array(15) {
[0]=>
  object(WP_Post)#285 (24) {
    ["ID"]=>
    int(253)
    ["post_author"]=>
    string(1) "1"
    ["post_date"]=>
    string(19) "2014-04-17 18:36:27"
    ["post_date_gmt"]=>
    string(19) "2014-04-17 18:36:27"
    ["post_content"]=>
    string(8) "gsdljdkf"
    ["post_title"]=>
    string(10) "Shortcoded"
    ["post_excerpt"]=>
    string(0) ""
    ["post_status"]=>
    string(7) "publish"
    ["comment_status"]=>
    string(6) "closed"
    ["ping_status"]=>
    string(4) "open"
    ["post_password"]=>
    string(0) ""
    ["post_name"]=>
    string(12) "shortcoded-2"
    ["to_ping"]=>
    string(0) ""
    ["pinged"]=>
    string(0) ""
    ["post_modified"]=>
    string(19) "2014-04-17 18:36:27"
    ["post_modified_gmt"]=>
    string(19) "2014-04-17 18:36:27"
    ["post_content_filtered"]=>
    string(0) ""
    ["post_parent"]=>
    int(0)
    ["guid"]=>
    string(51) "http://XXX.0.0.1:4001/wordpress/board/shortcoded-2/"
    ["menu_order"]=>
    int(0)
    ["post_type"]=>
    string(10) "board_post"
    ["post_mime_type"]=>
    string(0) ""
    ["comment_count"]=>
    string(1) "0"
    ["filter"]=>
    string(3) "raw"
  }
  [1]=>
  object(WP_Post)#284 (24) {
    (so on and so forth for all posts)
}

我有一个数组,其中包含要排除的帖子ID.

And I have an array with ids of posts to exclude.

$ ids_of_posts_to_exclude:

$ids_of_posts_to_exclude:

array(3) {
  [0]=> string(253)
  [1]=> string(456)
  [2]=> string(789)
}

如何从 $ posts 中删除 $ ids_of_posts_to_exclude 中列出的ID?

How to delete the posts which ids are listed in $ids_of_posts_to_exclude from $posts?

推荐答案

您可以使用foreach循环和 in_array()函数执行此操作.像这样:

You can just do this using a foreach loop and an in_array() function. Like this:

foreach($posts as $key => $post) {
    // if post id is inside the ids of posts to be excluded
    if(in_array($post->ID, $ids_of_posts_to_exclude)) {
        unset($posts[$key]); // remove that key
    }
}

这篇关于从多维数组中删除多个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆