0

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 Answers3

0

You can simply use retailer as key and append elements to it.

So, there would be 3 elements under HYD-48AC

and one element under PUN-48AC

<?php 
$arr = 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'
    )

    );
$groupedArr = [];
if (! empty($arr)) {
 foreach ($arr as $elem) {
  $groupedArr[$elem['retailer']][] = $elem;
 }
}
echo '<pre>';
print_r($groupedArr);
echo '</pre>';
?>

Output:

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

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

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

        )

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

        )

)

Hope it works.

Pupil
  • 23,834
  • 6
  • 44
  • 66
0

You can create a separate array of retailer batch id, where retailer group is the key and value is the batch id and use array_walk to iterate through and assign the related retailer batch id to products.

$retailers = ['HYD-48AC' => 'B001', 'PUN-48AC' => 'B002'];
array_walk($products, function(&$v, $k) use ($retailers){
  !empty($retailers[$v['retailer']]) ? ($v['batchid'] = $retailers[$v['retailer']]) : '';
});
Rakesh Jakhar
  • 6,380
  • 2
  • 11
  • 20
0

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.

Ronak Chauhan
  • 681
  • 1
  • 8
  • 22