Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

first let me explain what I am trying to achieve:

I've got an array of user items, consisting of an ID(item_id) and quantity(e.g. 10 items) If a user purchases an item, it's added to the array including the quantity. If a user purchases an (in the array) existing item, '1' is added to the quantity.

I got really close with the help of this post: Checking if array value exists in a PHP multidimensional array

this is the code I am using right now:

 $item_id = arg(1);
  $quantity = '1';

  $found = false;
  $bought_items = null;
  $data = null;

  foreach ($user_items as $key => $data) {

    if ($data['a'] == $item_id) {
        // The item has been found => add the new points to the existing ones
        $data['b'] += 1;
        $found = true;
        break; // no need to loop anymore, as we have found the item => exit the loop
    } 
  }

  if ($found === false) {

      $bought_items = array('a' => $item_id, 'b' => $quantity);
  }

  $array = array($bought_items, $data);

If the item_id is non existing, it is added to the array If the item_id is existing, the quantity will 'receive' +1

so far so good

now the actual problem, let's sketch the scenario:

I purchase item 500 -> array contains: id=500, quantity=1

I purchase item 500 -> array contains: id=500, quantity=2

I purchase item 600 -> array contains: id=500, quantity=2, id=600, quantity=1

after this it goes wrong

I then purchase item 500 or 600, the other item is removed from the array. So when I purchase item 500, item 600 and its quantities are removed from the array.

I've been puzzling for hours but can't find the mistake, I know I'm overlooking something logical. I think it's going wrong in the for each.

share|improve this question

2 Answers 2

up vote 1 down vote accepted

I tried for example this, and it works, so u can change to own use. The code of another post is useless for your purpose

    $item_id = 500;
    $quantity = 1;

    $user_items = array(400, 300, 200, 500, 500, 200, 500, 500);
    $found = FALSE;
    $bought_items = null;
    $data = null;

    foreach ($user_items as $data) {

        if ($data == $item_id) {
            // The item has been found => add the new points to the existing ones
            $quantity += 1;
            $bought_items[$data]['a'] = $data;
            $bought_items[$data]['b'] = $quantity;
            $found = TRUE;
        }

        if ($found === FALSE) {

            $bought_items[$data] = array('a' => $data, 'b' => $quantity);
        }
        $found = FALSE;
    }
    print_r($bought_items);

Output:

array(4) {
   400 => array(2) {
      a => 400
      b => 1
   }
   300 => array(2) {
      a => 300
      b => 1
   }
   200 => array(2) {
      a => 200
      b => 3
   }
   500 => array(2) {
      a => 500
      b => 5
   }
}
share|improve this answer

If bought_items is an array then you're overriding your values rather then adding them to the array.

$bought_items = array('a' => $item_id, 'b' => $quantity);

should be:

$bought_items[] = array('a' => $item_id, 'b' => $quantity);
share|improve this answer
    
the problem remains.. are you sure there are no other 'mistakes' in the code? –  Maarten Hartman Jan 28 '13 at 20:40
    
btw the error log contains: Notice: Undefined index: a (this is the item_id index) –  Maarten Hartman Jan 28 '13 at 21:06

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.