dqiz20794 2019-05-02 20: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-02 22: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条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部