Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations IamaSherpa on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Remove items from Array of objects on object keys

Status
Not open for further replies.

timgerr

IS-IT--Management
Jan 22, 2004
364
US
Hello all, I have an array of objects that I need to remove some items and keep others. Here is my problem, I have this array::
Code:
array
  0 => 
    object(stdClass)[9]
      public 'id' => string '781' (length=3)
      public 'root' => string '781' (length=3)
      public 'RSS_Role' => string '6' (length=1)
      public 'item' => string 'Bike' (length=4)
      public 'RES_TYPE' => string 'cbstree' (length=7)
    
  1 => 
    object(stdClass)[11]
      public 'id' => string '782' (length=3)
      public 'root' => string '781' (length=3)
      public 'RSS_Role' => string '6' (length=1)
      public 'item' => string 'happy' (length=4)
      public 'RES_TYPE' => string 'cbstree' (length=7)
      
  2 => 
    object(stdClass)[8]
      public 'res_resource' => string '516' (length=3)
      public 'groupname' => string '359' (length=3)
      public 'id' => string '781' (length=3)
      public 'item' => string 'Girl' (length=4)
      public 'RSS_ROLE' => string '6' (length=1)
  3 => 
    object(stdClass)[15]
      public 'id' => string '782' (length=3)
      public 'root' => string '781' (length=3)
      public 'RSS_Role' => string '6' (length=1)
      public 'item' => string 'tony' (length=4)
      public 'RES_TYPE' => string 'cbstree' (length=7)    
  4 => 
    object(stdClass)
      public 'res_resource' => string '225' (length=3)
      public 'groupname' => string '55' (length=3)
      public 'id' => string '781' (length=3)
      public 'item' => string 'Ouch' (length=4)
      public 'RSS_ROLE' => string '1' (length=1)
  5 => 
    object(stdClass)
      public 'id' => string '782' (length=3)
      public 'root' => string '781' (length=3)
      public 'RSS_Role' => string '3' (length=1)
      public 'item' => string 'DoomsDay' (length=4)
      public 'RES_TYPE' => string 'cbstree' (length=7)
I need to match these keys up
Code:
id
root
RES_TYPE
and find the best match by largest number on this key
Code:
RSS_ROLE

If my array is called meAr here is how the matches will look
Code:
if(meAr[0]->id == meAr[1]->id && 
   meAr[0]->root == meAr[1]->root &&
   meAr[0]->RES_TYPE == meAr[1]->RES_TYPE ){
      if(meAr[0]->RSS_ROLE > meAr[1]->RSS_ROLE){
          "REMOVE meAr[1]"
      }
      if(meAr[0]->RSS_ROLE < meAr[1]->RSS_ROLE){
          "REMOVE meAr[0]"
      }
      if(meAr[0]->RSS_ROLE == meAr[1]->RSS_ROLE){
          "REMOVE meAr[0]"
      }
}
[code]
I need to remove any duplicates and keep the objects that RSS_ROLE is the greatest.  How can I do this, I am killing myself.

thanks,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered? 
Congratulations!
 
can i clarify?

you
+ first want to find the largest value for RSS_ROLE
+ second extract the subset of the array which has RSS_ROLE equal to the largest value
+ then deduplicate the subset on RES_TYPE AND id AND root (i.e. if all three match then it is a duplicate but if any one or two match then it is not a duplicate)

 
That is correct, you are good.

timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
actually as i re-read your first post and my clarification I am not so sure that I have correctly understood your question.

I now think that what you may be asking is:

1. deduplicate on a match of RES_TYPE, id and root
2. of the duplicates, keep the one with the higher RSS_ROLE

keep all data points that are NOT duplicates.

my route to a solution would (for either scenario) to:

1. walk the array creating three arrays of id, RES_TYPe and root with the value as the key and the key of the original array as the value (in a sub array).
2. calculate the intersection of those three arrays which will give you the locations in the original array of objects that are duplicates
3. iterate those originals to find the highest RSS_ROLE and delete as necessary.

in item 2, the intersection calculation needs to be done iteratively against each value in the array. array_intersect will not, I think, work. there is probably a neat way of doing this with one of the array functions but a simple test against array_value() will also work.
 
I hate to ask for this but could you give an example, I am not sure how to do #1.

Thanks,
timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
i have no test data in a usable format but something like this should be nearly right

Code:
<?php

class handler{

	/**
	 * @var $data  holding array
	 */
	private $data =array();
	
	public function __construct($array){
		$final = array();
		$this->wind($array);
		//find the duplicates
		//cannot use array_unique.
		foreach ($this->data as $key=>$val){
			$results = array_keys($this->data, $val);
			if (count($results) > 1){
				$max = 0;
				$_key = null;
				foreach ($results as $key){
					if ($this->data[$key]['RSS_ROLE'] > $max){
						$max = $this->data[$key]['RSS_ROLE'];
						$_key = $key;	
					}
				}
				$final[$_key] = $array[$key];
			} else {
				$final[$key] = $array[$key];
			}
		}
		return $final;
	}
	
	/**
	 * walks the input array of objects and compiles information
	 * @param object $array
	 * @return 
	 */
	private function wind(&$array){
		foreach ($array as $key=>$obj):
			foreach(array('id', 'root', 'RES_TYPE') as $item){
				if (isset($obj->$item)){
					$this->data[strval($key)][$item] = $obj->$item;
				}
			}
		endforeach;
	}
	
}

$h = new handler($array);
print_r($h);
?>
 
Thanks for the help, I will give this a try. Have a good holiday.

timgerr

-How important does a person have to be before they are considered assassinated instead of just murdered?
Congratulations!
 
i should really have asked at the start why you wanted to do this and where the data comes from. restructuring data stores in mid-flow is seldom a good idea and equally is not always necessary if you can get the data shape right at the start of the process.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top