2013-10-23 71 views
0

我試圖找到使用其他屬性數組的數組內的對象(或多個對象)PHP:使用屬性陣列

我碰到這個問題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; 
} 

如何修改該行以測試所有給定的$屬性?

回答

1

注意,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; 
} 
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" 
    } 
} 
1
//$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; 
     } 
); 
0

我希望這將有助於你

陣列 ( [945] =>構件對象 ( [ID] => 13317 [名稱] =>測試999 [姓氏] =>測試999 ) [54] =>構件對象 ( [ID] => 13316 [名稱] =>曼努埃爾 [姓氏] =>瑪利亞帕拉 ) [654] =>構件對象 ( [ID] => 13315 [名稱] =>拜倫 [last_name] =>卡斯蒂略 ) [656] =>構件對象 ( [ID] => 13314 [名稱] =>塞薩爾 [姓氏] => Vasquez的 ) )

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'));