0

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?

4
  • 3
    I'd put it in a database. Commented Feb 1, 2013 at 19:06
  • Use the product name as the array index Commented Feb 1, 2013 at 19:07
  • 3
    Further than just the name as index - make each product name an index to a sub-array, and each product color a sub-array of that, which contains the sizes as keys. So $products['A']['RED']['XL'] (really though, it would be simplest in a database) Commented Feb 1, 2013 at 19:08
  • all products and qty are read from database. now want to display to users given conditions Commented Feb 1, 2013 at 19:18

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

3 Comments

no, I do not want to update to db. once read from db. I need to perform totally different operation. I need to re-distribute them by given condition. back to your code, in foreach statement, how to match product and color after find it how to update qty for that matched item?
I guess this is what you meant foreach($product_array as &$p) { if( $p['product'] == 'A' && $p['color'] == 'RED' ) { $qty = $p['XS']+$p['S']+$p['M']+$p['L']+$p['XL']; } }
foreach loop is just too create $product_by_name_and_color, an array of references. next bit of code shows how to update products.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.