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条)

报告相同问题?

悬赏问题

  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度
  • ¥30 关于#r语言#的问题:如何对R语言中mfgarch包中构建的garch-midas模型进行样本内长期波动率预测和样本外长期波动率预测