Reputation: 77
Let's say, I have something like below (quantities can be variable):
Product Color XS S M L XL
A RED 100 200 200 200 100
A WHITE 50 100 200 100 20
B RED 50 200 200 100 50
B WHITE 100 200 300 200 100
If I saved them into array such as like this:
$product_array[] =
array('product' => 'A', 'color' => 'RED',
'XS' => 100, 'S' => 200,
'M' => 200, 'L' => 200,
'XL' =>100);
What is the easiest way to find size quantity and update their given Product id and color? Let's say, (A, RED) sold XS size 10 qty?
Upvotes: 0
Views: 103
Reputation: 3695
As noted in comments you could use item name + color as an index. If you need to use given format you can create array of references to fasten things up.
$product_array[] =
array('product' => 'A', 'color' => 'RED',
'XS' => 100, 'S' => 200,
'M' => 200, 'L' => 200,
'XL' =>100);
$product_array[] =
array('product' => 'B', 'color' => 'GREEN',
'XS' => 100, 'S' => 200,
'M' => 200, 'L' => 200,
'XL' =>100);
foreach($product_array as &$product)
{
$product_by_name_and_color[$product['product']][$product['color']] =& $product;
}
and then:
$product_by_name_and_color['B']['GREEN']['XS'] += 5;
print_r($product_array);
EDIT:
If your products are red from database, most probably you want to update values in database and you are asking the wrong question.
Upvotes: 1