我有一个数组,在其中需要为属于不同零售商的产品分配唯一的批次ID
foreach($data as $type){
$grouped_types[$type['retailer']][] = $type;
}
$ data是我的数组
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
)
预期输出 因此数组2和3属于同一零售商,因此它们具有相同的批次ID。我需要为每个差异零售商分配唯一的批次ID,但是同一零售商下的产品应具有相同的批次ID。请帮助。
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
)
答案 0 :(得分:1)
第1步
采用零售商数组的键值对,如下例所示
//Where key as retailer id and value as batch id
$retailers = array(
"HYD-48AC" => "B001",
"PUN-48AC" => "B001",
);
现在在您的foreach循环中,请按照以下逻辑进行应用
foreach($data as $type){
$batch_id = $retailers[$type['retailer'];
$type['batchid'] = $batch_id;
$grouped_types[$type['retailer']][] = $type;
}
这将100%为您服务。
答案 1 :(得分:0)
您可以简单地使用retailer
作为键并将元素添加到其上。
因此,HYD-48AC
下将包含3个元素
和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>';
?>
输出:
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
)
)
)
希望它能起作用。
答案 2 :(得分:0)
您可以创建一个单独的零售商批次ID数组,其中零售商组是jdbcDF2.write()
.jdbc("jdbc:postgresql:dbserver", "schema.tablename", connectionProperties);
,而key
是批次ID,并使用value
来遍历并分配相关的零售商批次产品的ID。
array_walk