php 如何过滤对象数组?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/7826358/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 03:27:00  来源:igfitidea点击:

How to filter an array of object?

phparrays

提问by Alex

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.

1- 检查索引是否存在 2- 如果存在,检查所需的值,如果对象的索引等于该值,则删除整个对象。

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->membervalue is equal to 11, remove the object and return the array without this object. The returned array would thus be :

如果有一个对象的admins->member值等于 11,则删除该对象并返回没有此对象的数组。因此返回的数组将是:

        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?

我该怎么做?

回答by netcoder

Want to filter an array? Use array_filter!

想要过滤数组?使用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;
});

回答by Clive

You can use array_filterwith a custom callback:

您可以使用array_filter自定义回调:

function filter_callback($element) {
  if (isset($element->foo) && $element->foo == 'some_value') {
    return TRUE;
  }
  return FALSE;
}

$arr = array_filter($arr, 'filter_callback');

回答by aDhaouadi

Provide another approach with using case : function used array_key_exists& in_array

提供另一种使用 case 的方法:函数使用array_key_exists& in_array

$params = [
    "nom" => 'titof',
    "prenom" => 'edoe',
    "age" => '25'
    ];

$data = [
    0 => [
        'nom' => 'john',
        'prenom' => 'doe',
        'age' => '25'
        ],
    1 => [
        'nom' => 'lola',
        'prenom' => 'la grandi',
        'age' => '72'
        ],
    2 => [
        'nom' => 'michael',
        'prenom' => 'Pamal',
        'age' => '25'
        ]
    ];

function getFilteredArray($params, $data) {
   $filtredArray = [];
   foreach($params as $key => $value) {
       foreach($data as $index => $item) {
           if(array_key_exists($key, $item) && in_array($value, $params)) {
               if($item[$key] == $value ){
                   $filtredArray[$index] = $item;
               } else {
                   continue;
               }
           }
       }
   }
   return $filtredArray;
}

You can found it here : https://gist.github.com/ADCPD/a00df3f44b96322fbd6818f216e9d982

你可以在这里找到它:https: //gist.github.com/ADCPD/a00df3f44b96322fbd6818f216e9d982

回答by W. Dan

Provide another method:

提供另一种方法:

public function getArrayFiltered($aFilterKey, $aFilterValue, $array) {
    $filtered_array = array();
    foreach ($array as $value) {
        if (isset($value->$aFilterKey)) {
            if ($value->$aFilterKey == $aFilterValue) {
                $filtered_array[] = $value;
            }
        }
    }

    return $filtered_array;
}