dqdtgiw4736 2013-01-28 20:19
浏览 38
已采纳

PHP:每个之后丢失的数组值

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.

  • 写回答

2条回答 默认 最新

  • dongyanfeng0546 2013-01-29 01:17
    关注

    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
       }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效