0

I'm trying to find an object (Or multiple objects) inside an array using another attributes array

i came across this question Find array key in objects array given an attribute value and found great solution, i modified it a bit and end up with this:

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

How can modify that line to test all the given $attributes?

4 Answers 4

1

array_filter is dedicated method to filter arrays:

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

output:

array(1) {
  [1]=>
  object(stdClass)#4 (2) {
    ["first_name"]=>
    string(5) "value"
    ["last_name"]=>
    string(5) "value"
  }
}
Sign up to request clarification or add additional context in comments.

Comments

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

Comments

1

Pay attention that get_object_vars see only public attributes. May be more effective ReflectionClass::getProperties.

Coded in editor, to test:

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

Comments

0

I Hope It will help you

Array ( [945] => member Object ( [id] => 13317 [name] => Test 999 [last_name] => Test 999 ) [54] => member Object ( [id] => 13316 [name] => Manuel [last_name] => Maria parra ) [654] => member Object ( [id] => 13315 [name] => Byron [last_name] => Castillo ) [656] => member Object ( [id] => 13314 [name] => Cesar [last_name] => 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'));

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.