473,387 Members | 1,863 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,387 software developers and data experts.

How to compare values within an array

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

I have an array:
Expand|Select|Wrap|Line Numbers
  1. array(product_type, product_color, quantity);
For example - I have got 3 items in this array:
Expand|Select|Wrap|Line Numbers
  1. array('bmw', 'green', '4');
  2. array('audi', 'red', '6');
  3. 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:

Expand|Select|Wrap|Line Numbers
  1. array('bmw', 'green', '11');
  2. array('audi', 'red', '6');

Can anybody help me please?
thank you.
Feb 28 '10 #1
5 7608
Atli
5,058 Expert 4TB
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.
Feb 28 '10 #2
Thanks for your help.
Here is what I have done to solve the task:
Expand|Select|Wrap|Line Numbers
  1. $add_product = '';
  2. $add_product = array();
  3.  
  4. foreach($products as $key=>$values){
  5.  
  6.          $match=0;
  7.           foreach($add_product as $key2=>$values2){
  8.            $items=$values2;
  9.              foreach( $items as $key3=>$values3){
  10.                if ($values['product_type']==$values3['product_type']&&$values['product_color']==$values3['product_color']){
  11.                 $match=1;
  12.                 $quantity_new = $values['quantity']+$values3['quantity'];
  13.                 array_splice($add_product, $key);
  14.  
  15.                 $this->data['add_product'][$key3]['product_type'] = $values3['product_type'];
  16.                 $this->data['add_product'][$key3]['product_color'] = $values3['product_color'];
  17.                 $this->data['add_product'][$key3]['quantity'] = $quantity_new;
  18.                 $add_product = array ('add_product'=>$this->data['add_product']);                        
  19.                }
  20.               }
  21.              }
  22.           if ($match==0) {
  23.  
  24.            $this->data['add_product'][$key]['product_type'] = $values['product_type'];
  25.            $this->data['add_product'][$key]['product_color'] = $values['product_color'];
  26.            $this->data['add_product'][$key]['quantity'] = $values['quantity'];
  27.            $add_product = array ('add_product'=>$this->data['add_product']);
  28.           }
  29.  
  30. }
  31.  
  32.  
I hope this will help somebody. Also if you comment it - I would appreciate.
Mar 1 '10 #3
@intosana
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
Jul 28 '10 #4
Here is possible solution.
Expand|Select|Wrap|Line Numbers
  1.             $add_product = '';
  2.             $referenceid = '';
  3.             $add_product = array();
  4.  
  5.             foreach($products as $key=>$values){
  6.  
  7.                      $match=0;
  8.                       foreach($add_product as $key2=>$values2){
  9.                        $items=$values2;
  10.                          foreach( $items as $key3=>$values3){
  11.                            if ($values['product_type']==$values3['product_type']&&$values['product_color']==$values3['product_color']){
  12.                             $match=1;
  13.  
  14.                             //we set refferance unique number for matching items 
  15.                             $referenceid = $key3;
  16.  
  17.                             $this->data['add_product'][$key3]['product_type'] = $values3['product_type'];
  18.                             $this->data['add_product'][$key3]['product_color'] = $values3['product_color'];
  19.                             $this->data['add_product'][$key3]['quantity'] = $values3['quantity'];
  20.                             $this->data['add_product'][$key3]['reference'] = $referenceid;
  21.  
  22.                             $add_product = array ('add_product'=>$this->data['add_product']);                        
  23.                            }
  24.                           }
  25.                          }
  26.                       if ($match==0) {
  27.  
  28.                         //no need to add match id
  29.                        $referenceid ='';
  30.  
  31.                        $this->data['add_product'][$key]['product_type'] = $values['product_type'];
  32.                        $this->data['add_product'][$key]['product_color'] = $values['product_color'];
  33.                        $this->data['add_product'][$key]['quantity'] = $values['quantity'];
  34.                        $this->data['add_product'][$key3]['reference'] = $referenceid;
  35.  
  36.                        $add_product = array ('add_product'=>$this->data['add_product']);
  37.                       }
  38.  
  39.             }
Jul 28 '10 #5
@intosana
Hi , intosana, thanks for your reply ... but i didn't get my desired out put.
here is my complete code
Expand|Select|Wrap|Line Numbers
  1. $products = array(
  2.                    array('product_type'=>'bmw', 'product_color'=>'green', 'quantity'=>'4'),
  3.                    array('product_type'=>'audi', 'product_color'=>'red', 'quantity'=>'6'),
  4.                    array('product_type'=>'bmw', 'product_color'=>'green', 'quantity'=>'7'),
  5.                    array('product_type'=>'audi', 'product_color'=>'red', 'quantity'=>'1'),
  6.                    array('product_type'=>'audi', 'product_color'=>'blue', 'quantity'=>'2'),
  7.                    array('product_type'=>'skoda', 'product_color'=>'blue', 'quantity'=>'2'),
  8.                  );
  9.  
  10.  
  11.  
  12.             $add_product = '';
  13.             $referenceid = '0';
  14.             $add_product = array();
  15.  
  16.             foreach($products as $key=>$values){
  17.  
  18.                      $match=0;
  19.                       foreach($add_product as $key2=>$values2){
  20.                        $items=$values2;
  21.                          foreach( $items as $key3=>$values3){
  22.                            if ($values['product_type']==$values3['product_type']){
  23.                             $match=1;
  24.  
  25.                             //we set refferance unique number for matching items
  26.                             $referenceid = $key3;
  27.  
  28.                             $data['add_product'][$key3]['product_type'] = $values3['product_type'];
  29.                             $data['add_product'][$key3]['product_color'] = $values3['product_color'];
  30.                             $data['add_product'][$key3]['quantity'] = $values3['quantity'];
  31.                             $data['add_product'][$key3]['reference'] = $referenceid;
  32.  
  33.                             $add_product = array ('add_product'=>$data['add_product']);
  34.                            }
  35.                           }
  36.                          }
  37.                       if ($match==0) {
  38.  
  39.                         //no need to add match id
  40.                        $referenceid ='0';
  41.  
  42.                        $data['add_product'][$key]['product_type'] = $values['product_type'];
  43.                        $data['add_product'][$key]['product_color'] = $values['product_color'];
  44.                        $data['add_product'][$key]['quantity'] = $values['quantity'];
  45.                        $data['add_product'][$key]['reference'] = $referenceid;
  46.  
  47.                        $add_product = array ('add_product'=>$data['add_product']);
  48.                       }
  49.  
  50.             }
  51.  
and the desired out put need to be
Expand|Select|Wrap|Line Numbers
  1. array(
  2.                    array('product_type'=>'bmw', 'product_color'=>'green', 'quantity'=>'4', 'referenceid'=>'123'),
  3.                    array('product_type'=>'audi', 'product_color'=>'red', 'quantity'=>'6', 'referenceid'=>'456'),
  4.                    array('product_type'=>'bmw', 'product_color'=>'green', 'quantity'=>'7', 'referenceid'=>'123'),
  5.                    array('product_type'=>'audi', 'product_color'=>'red', 'quantity'=>'1','referenceid'=>'456'),
  6.                    array('product_type'=>'audi', 'product_color'=>'blue', 'quantity'=>'2', 'referenceid'=>'456'),
  7.                    array('product_type'=>'skoda', 'product_color'=>'blue', 'quantity'=>'2', 'referenceid'=>'0'),
  8.                  );
  9.  
ie, don't combine matching result, instead put a referenceid to identify matching results uniquely.
waiting for your reply
Jul 28 '10 #6

Sign in to post your reply or Sign up for a free account.

Similar topics

0
by: Phil Powell | last post by:
/*-------------------------------------------------------------------------------------------------------------------------------- Parameters: $formField1: The name of the first array $formField2:...
4
by: Gleep | last post by:
Hey Guys, I've got a table called Outcomes. With 3 columns and 15 rows 1st col 2nd col 3rdcol outcome date price There are 15 rows...
30
by: Christian Seberino | last post by:
How does Ruby compare to Python?? How good is DESIGN of Ruby compared to Python? Python's design is godly. I'm wondering if Ruby's is godly too. I've heard it has solid OOP design but then...
3
by: domeceo | last post by:
can anyone tell me why I cannot pass values in a setTimeout function whenever I use this function it says "menu is undefined" after th alert. function imgOff(menu, num) { if (document.images) {...
16
by: michael | last post by:
Is it possible to get all href URLs contained in a unordered list and place them in an array? Or in fact two different arrays, differently named one for each <ul> group? <ul> <li><a...
8
by: Kenneth Baltrinic | last post by:
I am trying to compare values coming out of a database record with known default values. The defaults are in an array of type object (because they can be of any basic data type, I am not working...
5
by: Jason | last post by:
Is there a mechanism in VB.NET that allows something like: If myVar In ("A","B","C") Then... The way I'm doing it now is: Select Case myVar Case "A","B","C" Or like this:
3
by: John | last post by:
I have two large arrays. Is there a rapid method to comaprs the two arrays and creat a third array of onlt thos items that the are in the first array but not the second array. I do it manually...
3
by: =?Utf-8?B?UmljY2FyZG8=?= | last post by:
What is the best method to compare 2 array to knw if each element is equal? Now, i have to compare the datarow.itemarray of 2 datarows wich is equals in structure. I tried to write this routine:...
4
by: toadstool | last post by:
I have an array containing the various products $inventory = array(); $inventory = "whatever1"; etc I have an array containing various values $quantity = array(100,200,300); I call in a...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.