How to compare values within an array

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • intosana
    New Member
    • Feb 2010
    • 3

    How to compare values within an array

    Good day,
    I would appreciate your help with my question:

    I have an array:
    Code:
    array(product_type, product_color, quantity);
    For example - I have got 3 items in this array:
    Code:
    array('bmw', 'green', '4');
    array('audi', 'red', '6');
    array('bmw', 'green', '7');
    My question is: How can I go thought this array and compare values - searching for those, who have the same 'product_type' and 'product_color' and then to create new array with those values merged and their 'quantity' added.

    this is what I would get in result using my example:

    Code:
    array('bmw', 'green', '11');
    array('audi', 'red', '6');

    Can anybody help me please?
    thank you.
    Last edited by Atli; Feb 28 '10, 08:19 PM. Reason: Added [code] tags.
  • Atli
    Recognized Expert Expert
    • Nov 2006
    • 5062

    #2
    Hey.

    Check out the foreach loop or the for loop. You can use them to loop through all your array elements, compare them, and modify them if needed.

    Comment

    • intosana
      New Member
      • Feb 2010
      • 3

      #3
      resolved

      Thanks for your help.
      Here is what I have done to solve the task:
      Code:
      $add_product = '';
      $add_product = array();
      
      foreach($products as $key=>$values){
      
      		 $match=0;
      		  foreach($add_product as $key2=>$values2){
      		   $items=$values2;
      			 foreach( $items as $key3=>$values3){
      			   if ($values['product_type']==$values3['product_type']&&$values['product_color']==$values3['product_color']){
      				$match=1;
      				$quantity_new = $values['quantity']+$values3['quantity'];
      				array_splice($add_product, $key);
      
      				$this->data['add_product'][$key3]['product_type'] = $values3['product_type'];
      				$this->data['add_product'][$key3]['product_color'] = $values3['product_color'];
      				$this->data['add_product'][$key3]['quantity'] = $quantity_new;
      				$add_product = array ('add_product'=>$this->data['add_product']);						
      			   }
      			  }
      			 }
      		  if ($match==0) {
      
      		   $this->data['add_product'][$key]['product_type'] = $values['product_type'];
      		   $this->data['add_product'][$key]['product_color'] = $values['product_color'];
      		   $this->data['add_product'][$key]['quantity'] = $values['quantity'];
      		   $add_product = array ('add_product'=>$this->data['add_product']);
      		  }
      	
      }
      I hope this will help somebody. Also if you comment it - I would appreciate.

      Comment

      • slekshmipriya
        New Member
        • Mar 2007
        • 7

        #4
        Originally posted by intosana
        Thanks for your help.
        Here is what I have done to solve the task:
        Code:
        $add_product = '';
        $add_product = array();
        
        foreach($products as $key=>$values){
        
        		 $match=0;
        		  foreach($add_product as $key2=>$values2){
        		   $items=$values2;
        			 foreach( $items as $key3=>$values3){
        			   if ($values['product_type']==$values3['product_type']&&$values['product_color']==$values3['product_color']){
        				$match=1;
        				$quantity_new = $values['quantity']+$values3['quantity'];
        				array_splice($add_product, $key);
        
        				$this->data['add_product'][$key3]['product_type'] = $values3['product_type'];
        				$this->data['add_product'][$key3]['product_color'] = $values3['product_color'];
        				$this->data['add_product'][$key3]['quantity'] = $quantity_new;
        				$add_product = array ('add_product'=>$this->data['add_product']);						
        			   }
        			  }
        			 }
        		  if ($match==0) {
        
        		   $this->data['add_product'][$key]['product_type'] = $values['product_type'];
        		   $this->data['add_product'][$key]['product_color'] = $values['product_color'];
        		   $this->data['add_product'][$key]['quantity'] = $values['quantity'];
        		   $add_product = array ('add_product'=>$this->data['add_product']);
        		  }
        	
        }
        I hope this will help somebody. Also if you comment it - I would appreciate.
        hello intosana,

        I too need something similar to ur requirement. What i want is , instead of grouping the matching products.
        i need to add a new key say 'referenceid' to the matching elements.

        I need the output like



        1. array('bmw', 'green', '4','123');
        2. array('audi', 'red', '6', '0');
        3. array('bmw', 'green', '7', '123');

        Please advice how to achieve this. Thanks in advance

        Comment

        • intosana
          New Member
          • Feb 2010
          • 3

          #5
          Here is possible solution.
          Code:
          			$add_product = '';
          			$referenceid = '';
          			$add_product = array();
          			 
          			foreach($products as $key=>$values){
          			 
          					 $match=0;
          					  foreach($add_product as $key2=>$values2){
          					   $items=$values2;
          						 foreach( $items as $key3=>$values3){
          						   if ($values['product_type']==$values3['product_type']&&$values['product_color']==$values3['product_color']){
          							$match=1;
          							
          							//we set refferance unique number for matching items 
          							$referenceid = $key3;
          
          							$this->data['add_product'][$key3]['product_type'] = $values3['product_type'];
          							$this->data['add_product'][$key3]['product_color'] = $values3['product_color'];
          							$this->data['add_product'][$key3]['quantity'] = $values3['quantity'];
          							$this->data['add_product'][$key3]['reference'] = $referenceid;
          
          							$add_product = array ('add_product'=>$this->data['add_product']);                        
          						   }
          						  }
          						 }
          					  if ($match==0) {
          
          						//no need to add match id
          					   $referenceid ='';
          
          					   $this->data['add_product'][$key]['product_type'] = $values['product_type'];
          					   $this->data['add_product'][$key]['product_color'] = $values['product_color'];
          					   $this->data['add_product'][$key]['quantity'] = $values['quantity'];
          					   $this->data['add_product'][$key3]['reference'] = $referenceid;
          
          					   $add_product = array ('add_product'=>$this->data['add_product']);
          					  }
          			 
          			}

          Comment

          • slekshmipriya
            New Member
            • Mar 2007
            • 7

            #6
            Originally posted by intosana
            Here is possible solution.
            Code:
            			$add_product = '';
            			$referenceid = '';
            			$add_product = array();
            			 
            			foreach($products as $key=>$values){
            			 
            					 $match=0;
            					  foreach($add_product as $key2=>$values2){
            					   $items=$values2;
            						 foreach( $items as $key3=>$values3){
            						   if ($values['product_type']==$values3['product_type']&&$values['product_color']==$values3['product_color']){
            							$match=1;
            							
            							//we set refferance unique number for matching items 
            							$referenceid = $key3;
            
            							$this->data['add_product'][$key3]['product_type'] = $values3['product_type'];
            							$this->data['add_product'][$key3]['product_color'] = $values3['product_color'];
            							$this->data['add_product'][$key3]['quantity'] = $values3['quantity'];
            							$this->data['add_product'][$key3]['reference'] = $referenceid;
            
            							$add_product = array ('add_product'=>$this->data['add_product']);                        
            						   }
            						  }
            						 }
            					  if ($match==0) {
            
            						//no need to add match id
            					   $referenceid ='';
            
            					   $this->data['add_product'][$key]['product_type'] = $values['product_type'];
            					   $this->data['add_product'][$key]['product_color'] = $values['product_color'];
            					   $this->data['add_product'][$key]['quantity'] = $values['quantity'];
            					   $this->data['add_product'][$key3]['reference'] = $referenceid;
            
            					   $add_product = array ('add_product'=>$this->data['add_product']);
            					  }
            			 
            			}
            Hi , intosana, thanks for your reply ... but i didn't get my desired out put.
            here is my complete code
            [code=php]
            $products = array(
            array('product_ type'=>'bmw', 'product_color' =>'green', 'quantity'=>'4' ),
            array('product_ type'=>'audi', 'product_color' =>'red', 'quantity'=>'6' ),
            array('product_ type'=>'bmw', 'product_color' =>'green', 'quantity'=>'7' ),
            array('product_ type'=>'audi', 'product_color' =>'red', 'quantity'=>'1' ),
            array('product_ type'=>'audi', 'product_color' =>'blue', 'quantity'=>'2' ),
            array('product_ type'=>'skoda', 'product_color' =>'blue', 'quantity'=>'2' ),
            );



            $add_product = '';
            $referenceid = '0';
            $add_product = array();

            foreach($produc ts as $key=>$values){

            $match=0;
            foreach($add_pr oduct as $key2=>$values2 ){
            $items=$values2 ;
            foreach( $items as $key3=>$values3 ){
            if ($values['product_type']==$values3['product_type']){
            $match=1;

            //we set refferance unique number for matching items
            $referenceid = $key3;

            $data['add_product'][$key3]['product_type'] = $values3['product_type'];
            $data['add_product'][$key3]['product_color'] = $values3['product_color'];
            $data['add_product'][$key3]['quantity'] = $values3['quantity'];
            $data['add_product'][$key3]['reference'] = $referenceid;

            $add_product = array ('add_product'= >$data['add_product']);
            }
            }
            }
            if ($match==0) {

            //no need to add match id
            $referenceid ='0';

            $data['add_product'][$key]['product_type'] = $values['product_type'];
            $data['add_product'][$key]['product_color'] = $values['product_color'];
            $data['add_product'][$key]['quantity'] = $values['quantity'];
            $data['add_product'][$key]['reference'] = $referenceid;

            $add_product = array ('add_product'= >$data['add_product']);
            }

            }
            [/code]

            and the desired out put need to be
            [code=php]
            array(
            array('product_ type'=>'bmw', 'product_color' =>'green', 'quantity'=>'4' , 'referenceid'=> '123'),
            array('product_ type'=>'audi', 'product_color' =>'red', 'quantity'=>'6' , 'referenceid'=> '456'),
            array('product_ type'=>'bmw', 'product_color' =>'green', 'quantity'=>'7' , 'referenceid'=> '123'),
            array('product_ type'=>'audi', 'product_color' =>'red', 'quantity'=>'1' ,'referenceid'= >'456'),
            array('product_ type'=>'audi', 'product_color' =>'blue', 'quantity'=>'2' , 'referenceid'=> '456'),
            array('product_ type'=>'skoda', 'product_color' =>'blue', 'quantity'=>'2' , 'referenceid'=> '0'),
            );
            [/code]

            ie, don't combine matching result, instead put a referenceid to identify matching results uniquely.
            waiting for your reply
            Last edited by Atli; Jul 28 '10, 05:26 PM. Reason: Please use [code] tags when posting code!

            Comment

            Working...