如何筛选对象的数组? [英] How to filter an array of object?

查看:136
本文介绍了如何筛选对象的数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经有了对象的数组(下面显示),我想编写一个返回同一阵列但随着对象(S)符合标准中删除的功能。

I've got an array of objects (show below) and I would like to write a function that returns the same array but with the "object(s)" that meet the criterion removed.

该函数将:

1检查
2-如果存在,为所需的值,并且如果对象的索引等于该值时,删除整个对象。检查

1- check if the index exists 2- if it exists, checks for the required value and if the object's index is equal to that value, remove the whole object.

例如:

    Array
(
    [course] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Programmation Web
                    [description] => 
                    [public] => 0
                    [requests] => 0
                    [id] => 245
                    [members] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 11
                                    [name] => Robert Smith
                                )

                        )

                    [projects] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 1923
                                    [title] => Sans titre (1)
                                    [type] => portfolio
                                )

                            )

                    [project_count] => 1
                    [admins] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [member] => 11
                                    [firstname] => Robert
                                    [lastname] => Smith
                                )

                        )

                    [topic_name] => Le PHP
                    [activites] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [topic_name] => 
                                    [topic_id] => 42
                                    [post_parent] => 107
                                    [post_body] => Oui moi aussi je me demande ça.
                                    [post_id] => 109
                                )

                        )

                    [forums] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [forum_name] => Discussion générale
                                    [forum_id] => 101
                                )

                        )

                )

            [1] => stdClass Object
                (
                    [name] => Les bases de données
                    [description] => 
                    [public] => 0
                    [jointype] => controlled
                    [grouptype] => course
                    [membershiptype] => admin
                    [topic_name] => Difficulté
                    [activites] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [topic_name] => 
                                    [topic_id] => 44
                                    [post_parent] => 111
                                    [post_body] => Ouah!
                                    [post_id] => 112
                                )

                        )

                    [forums] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [forum_name] => Le MySQL
                                    [forum_id] => 103
                                )

                        )

                )

        )

)

如果有一个对象,它的 admins->成员值等于11,取出对象,并没有这个对象返回数组。因此,返回数组将是:

If there's an object whose admins->member value is equal to 11, remove the object and return the array without this object. The returned array would thus be :

        Array
(
    [course] => Array
        (
            [0] => stdClass Object
                (
                    [name] => Programmation Web
                    [description] => 
                    [public] => 0
                    [requests] => 0
                    [id] => 245
                    [members] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 11
                                    [name] => Robert Smith (smithrobert)
                                )

                        )

                    [projects] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [id] => 1923
                                    [title] => Sans titre (1)
                                    [type] => portfolio
                                )

                            )

                    [project_count] => 1
                    [admins] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [member] => 11
                                    [firstname] => Robert
                                    [lastname] => Smith
                                )

                        )

                    [topic_name] => Le PHP
                    [activites] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [topic_name] => 
                                    [topic_id] => 42
                                    [post_parent] => 107
                                    [post_body] => Oui moi aussi je me demande ça.
                                    [post_id] => 109
                                )

                        )

                    [forums] => Array
                        (
                            [0] => stdClass Object
                                (
                                    [forum_name] => Discussion générale
                                    [forum_id] => 101
                                )

                        )

                )

        )

)

我怎么会去这样做?

How would I go about doing that ?

在此先感谢您的帮助! :O)

Thanks in advance for your help! :o)

推荐答案

要过滤数组?使用 array_filter

$new_array = array_filter($array, function($obj){
    if (isset($obj->admins)) {
        foreach ($obj->admins as $admin) {
            if ($admin->member == 11) return false;
        }
    }
    return true;
});

这篇关于如何筛选对象的数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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