如何过滤一个对象数组?

时间:2011-10-19 18:39:59

标签: php arrays

我有一个对象数组(如下所示),我想写一个函数返回相同的数组但是“object(s)”符合删除的标准。

该功能将:

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

例如:

    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的对象,请删除该对象并返回没有此对象的数组。因此返回的数组是:

        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
                                )

                        )

                )

        )

)

我将如何做到这一点?

4 个答案:

答案 0 :(得分:35)

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

答案 1 :(得分:3)

您可以将array_filter与自定义回调结合使用:

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

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

答案 2 :(得分:1)

通过用例提供另一种方法: 使用的功能array_key_existsin_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;
}

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

答案 3 :(得分:0)

提供另一种方法:

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;
}