dongshixingga7900 2016-05-17 10:30
浏览 15
已采纳

Foreach将特定产品添加到阵列

I had a previous problem which I have somewhat overcome. However this has resulted in a new problem which when fixed it should be working as intended.

I have created an array of Orders from a csv file and have managed to extract the data from the Orders (Products, Address etc.) and be printed together (Which was originally first problem). For example the csv file contains 2 Orders which prints them all together such as:

Order: Order1
Recipient: Timmy
Address: 123 Street
Products:
Table (Quantity: 1)
Cup (Quantity 4)

Order: Order2
Recipient: Billy
Address: 456 Street
Products:
Chair (Quantity 5)

The new problem is now this data needs to go back into multiple arrays because of the API which is being used. I have this nearly working apart from the products, which is currently adding products from the previous order. Using the above example, the array would be the following:

//Order1
array(2)
{
    [0]=> array(7)
    {
        ["Product"]=> string(13) "Table"
        ["Quantity"]=> float(1)
    } 

    [1]=> array(7)
    {
        ["Product"]=> string(13) "Cup"
        ["Quantity"]=> float(4)
    }
}

//Order2
array(3)
{
    [0]=> array(7)
    {
        ["Product"]=> string(13) "Table"
        ["Quantity"]=> float(1)
    } 

    [1]=> array(7)
    {
        ["Product"]=> string(13) "Cup"
        ["Quantity"]=> float(4)
    }

[2]=> array(7)
    {
        ["Product"]=> string(13) "Chair"
        ["Quantity"]=> float(5)
    }
} 

As shown above, the products from the previous order is duplicated onto the second order. The other order details are working as intended and being inserted as separate records into the database (Order Number, Address etc.). I'm unsure where I am going wrong, but this is the part of the code for the products array:

    foreach ($Order[$OrderNumber][$RecipientName][$Address1] as $key => $value)
    {   
        $APIOrderItem[] = array('Product' => $key,
                                'Quantity' => $value);
    }

$Items = array('APIOrderItem' => $APIOrderItem);

If previous code is required, I'll happily edit this post.

  • 写回答

1条回答 默认 最新

  • dongyan7988 2016-05-18 04:39
    关注

    try something like this

    check_key_value_exists($key,$value)
    {
    foreach($APIOrderItem as $row)
    {
    
    
      if($key==$row['Product'] && $value==$row['Quantity'])
        {
    
           return true;
    
        }
    
    
    }
    
    return false;
     }
    
    
    $i=1;
     foreach ($Order[$OrderNumber][$RecipientName][$Address1] as $key => $value)
    {   
    
       if($i==1)
       {
    
        $val=false;
       }
       else
       {
    
          $val = check_key_value_exists($key,$value);
    
         }
    
    
       if($val===false)
       {
          //new  $key and $value insert here 
    
        $APIOrderItem[] = array('Product' => $key,
                                'Quantity' => $value);
    
    
       }
       else
       {
    
            //$key and $value alreday exists in the $APIOrderItem array so dont need to insert again 
    
    
       }
    
       $i++;
    }
    
     $Items = array('APIOrderItem' => $APIOrderItem);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 vue3+element-plus页面崩溃
  • ¥15 像这种代码要怎么跑起来?
  • ¥15 怎么改成循环输入删除(语言-c语言)
  • ¥15 安卓C读取/dev/fastpipe屏幕像素数据
  • ¥15 pyqt5tools安装失败
  • ¥15 mmdetection
  • ¥15 nginx代理报502的错误
  • ¥100 当AWR1843发送完设置的固定帧后,如何使其再发送第一次的帧
  • ¥15 图示五个参数的模型校正是用什么方法做出来的。如何建立其他模型
  • ¥100 描述一下元器件的基本功能,pcba板的基本原理