Good day,
I would appreciate your help with my question:
I have an array: - array(product_type, product_color, quantity);
For example - I have got 3 items in this array: - 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: - array('bmw', 'green', '11');
-
array('audi', 'red', '6');
Can anybody help me please?
thank you.
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.
Thanks for your help.
Here is what I have done to solve the task: -
$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.
@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
Here is possible solution. -
$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']);
-
}
-
-
}
@intosana
Hi , intosana, thanks for your reply ... but i didn't get my desired out put.
here is my complete code -
$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($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']){
-
$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']);
-
}
-
-
}
-
and the desired out put need to be -
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'),
-
);
-
ie, don't combine matching result, instead put a referenceid to identify matching results uniquely.
waiting for your reply
Sign in to post your reply or Sign up for a free account.
Similar topics
by: Phil Powell |
last post by:
/*--------------------------------------------------------------------------------------------------------------------------------
Parameters:
$formField1: The name of the first array
$formField2:...
|
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...
|
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...
|
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) {...
|
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...
|
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...
|
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:
|
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...
|
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:...
|
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...
|
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,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
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$) {
}
...
|
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...
|
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...
|
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...
|
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...
|
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...
|
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...
| |