【问题标题】:PHP: find an object in array using attributes arrayPHP:使用属性数组在数组中查找对象
【发布时间】:2013-10-23 05:59:24
【问题描述】:

我正在尝试使用另一个属性数组在数组中查找一个对象(或多个对象)

我遇到了这个问题Find array key in objects array given an attribute value 并找到了很好的解决方案,我对其进行了一些修改并最终得到了这个:

//$array = array(object1,object2,object3);
//$attributes example array('first_name'=>'value','last_name'=>'value');
function filter_by_key($array, $attributes) {
   $filtered = array();
   foreach($array as $k => $v) {
      //if($v->$member != $value) //stuck here 
         $filtered[$k] = $v;
   }
   return $filtered;
}

如何修改该行以测试所有给定的 $attributes?

【问题讨论】:

    标签: php arrays


    【解决方案1】:

    array_filter 是过滤数组的专用方法:

    function createObject($first, $last) {
       $object = new StdClass;
       $object->first_name = $first;
       $object->last_name = $last;
       return $object;
    }
    
    $array = array( createObject('value1', 'value1'), createObject('value', 'value'));
    $attributes = array('first_name'=>'value','last_name'=>'value');
    
    var_dump(array_filter($array, function ($element) use($attributes) {
       foreach ($attributes as $attribute => $value) {
            if (is_object($element) 
                && property_exists($element, $attribute) 
                && $element->{$attribute} !== $value
            ) {
                return false;
            }
       }
       return true;
    }));
    

    输出:

    array(1) {
      [1]=>
      object(stdClass)#4 (2) {
        ["first_name"]=>
        string(5) "value"
        ["last_name"]=>
        string(5) "value"
      }
    }
    

    【讨论】:

      【解决方案2】:
      //$array = array(object1,object2,object3);
      //$attributes = array('first_name'=>'value','last_name'=>'value');
      $filtered = array_filter($array,
              // that's the callback function that filters the object 
              function ($e){
                  global $attributes; // we need to make $attributes 
                                      // recognizable in the scope
                  foreach($attributes as $k => $v){
                      if($e[$k] == $v){ // only if object $e from the array has 
                                        // the same attribute and same value
                          return true;  // add this object to $filtered
                      }
                  }
                  return false;
              }
      );
      

      【讨论】:

        【解决方案3】:

        注意 get_object_vars 只能看到 public 属性。 ReflectionClass::getProperties可能更有效。

        在编辑器中编码,进行测试:

        //$array = array(object1,object2,object3);
        //$attributes example array('first_name'=>'value','last_name'=>'value');
        function filter_by_key($array, $attributes) {
           $filtered = array();
           foreach($array as $obj) {
               $found = true;
               $obj_attr = get_object_vars($obj);
               foreach($attributes as $attr => $val){
                   if(!isset($obj_attr[$attr]) || $obj_attr[$attr] != $val){
                       $found = false;
                       break;
                   } 
               } 
               if($found){
                 $filtered[$k] = $obj;
               }
           }
           return $filtered;
        }
        

        【讨论】:

          【解决方案4】:

          希望对你有帮助

          数组 ( [945] => 成员对象 ( [id] => 13317 [名称] => 测试 999 [姓氏] => 测试 999 ) [54] => 成员对象 ( [id] => 13316 [名称] => 曼努埃尔 [姓氏] => 玛丽亚·帕拉 ) [654] => 成员对象 ( [id] => 13315 [名称] => 拜伦 [姓氏] => 卡斯蒂略 ) [656] => 成员对象 ( [id] => 13314 [名称] => 塞萨尔 [姓氏] => 瓦斯奎兹 ) )

          function filter_by_key($array, $member, $attributes) {
          $filtered = array();
          foreach ($array as $k => $v) {
              if (in_array($v->$member, $attributes)) {
                  $filtered[$k] = $v;
              }
          }
          return $filtered;
          

          }

          $filterd = filter_by_key($array, 'id', array('13316','13317'));
          

          【讨论】:

            猜你喜欢
            • 2018-10-31
            • 1970-01-01
            • 1970-01-01
            • 2012-12-01
            • 2011-05-09
            • 2014-11-22
            • 1970-01-01
            • 1970-01-01
            • 2020-06-20
            相关资源
            最近更新 更多