Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
323 views
in Technique[技术] by (71.8m points)

php - How to filter an array of object?

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.

The function would :

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.

For example :

    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
                                )

                        )

                )

        )

)

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

Want to filter an array? Use 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;
});

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...