dqiz20794 2019-05-03 04:13
浏览 38
已采纳

组数组并使用相同的键值分配唯一编号

I have an an array in which I need to assign unique batch id to products belongs to different Retailer

foreach($data as $type){
$grouped_types[$type['retailer']][] = $type;
}

$data is my array

Array
 ( 
[1] => Array
    (

        [productid] => 1001
        [mrp] => 444
        [whpoid] => 105
        [retailer] => HYD-48AC
        [manufacturerbarcode] => 
        [comments] => 
        [lastmodifiedby] => 
        [lastmodifieddate] => 2019-02-12 11:49:19
    )

[2] => Array
    (
        [productid] => 1002
        [mrp] => 444
        [whpoid] => 106
        [retailer] => HYD-48AC
        [manufacturerbarcode] => 
        [comments] => 
        [lastmodifiedby] => 
        [lastmodifieddate] => 2019-02-12 11:49:19
    )

[3] => Array
    (
        [productid] => 1003
        [mrp] => 444
        [whpoid] => 105
        [retailer] => HYD-48AC
        [manufacturerbarcode] => 
        [comments] => 
        [lastmodifiedby] => 
        [lastmodifieddate] => 2019-02-12 11:49:19
    )
 [4] => Array
    (
        [productid] => 1005
        [mrp] => 444
        [whpoid] => 105
        [retailer] => PUN-48AC
        [manufacturerbarcode] => 
        [comments] => 
        [lastmodifiedby] => 
        [lastmodifieddate] => 2019-02-12 11:49:19
    )

Expected Output So array 2 and 3 belongs to same retailer so they have same batch id.I need to assign unique batch id to each diff retailer but product under same retailer should have same batch id.Kindly help.Thanks in advance

Array
 ( 
  [1] => Array
  (

    [productid] => 1001
    [mrp] => 444
    [whpoid] => 105
    [retailer] => HYD-48AC
    [manufacturerbarcode] => 
    [comments] => 
    [lastmodifiedby] => 
    [lastmodifieddate] => 2019-02-12 11:49:19
    [batchid] => B001
  )

  [2] => Array
  (
    [productid] => 1002
    [mrp] => 444
    [whpoid] => 106
    [retailer] => HYD-48AC
    [manufacturerbarcode] => 
    [comments] => 
    [lastmodifiedby] => 
    [lastmodifieddate] => 2019-02-12 11:49:19
    [batchid] => B002
  )

 [3] => Array
   (
    [productid] => 1003
    [mrp] => 444
    [whpoid] => 105
    [retailer] => HYD-48AC
    [manufacturerbarcode] => 
    [comments] => 
    [lastmodifiedby] => 
    [lastmodifieddate] => 2019-02-12 11:49:19
    [batchid] => B002
   )
  [4] => Array
  (
    [productid] => 1005
    [mrp] => 444
    [whpoid] => 105
    [retailer] => PUN-48AC
    [manufacturerbarcode] => 
    [comments] => 
    [lastmodifiedby] => 
    [lastmodifieddate] => 2019-02-12 11:49:19
    [batchid] => B003
  )
  • 写回答

3条回答 默认 最新

  • dtwk6019 2019-05-03 06:43
    关注

    Step 1

    Take key value pair of retailer array like example given below

    //Where key as retailer id and value as batch id
    $retailers = array(
         "HYD-48AC" => "B001",
         "PUN-48AC" => "B001",
    ); 
    

    Now in you foreach loop apply below logic

    foreach($data as $type){
        $batch_id = $retailers[$type['retailer'];
        $type['batchid'] = $batch_id;
        $grouped_types[$type['retailer']][] = $type;
    }
    

    This will 100% work for you.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向