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 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值