Php 如何使用所有数组中的单个唯一值从多维数组中删除重复值
我有如下的多维数组Php 如何使用所有数组中的单个唯一值从多维数组中删除重复值,php,arrays,multidimensional-array,array-unique,Php,Arrays,Multidimensional Array,Array Unique,我有如下的多维数组 $product = array( "2e471a22b1b994a7cb3f3a40cee9fba2" => array ( "product" => 6004, "unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab, "product_id" => 51, "line
$product = array(
"2e471a22b1b994a7cb3f3a40cee9fba2" => array (
"product" => 6004,
"unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab,
"product_id" => 51,
"line_total"=>99,
"quantity"=>1,
"data"=> array(
"id"=> 51,
"post"=>array(
"ID"=>51,
"post_title"=>"Prodcut four - control",
),
"price"=>99
)
),
"a7d0f813832ec8a2bf24269ff7145d0c" => array (
"product" => 6004,
"unique_key" => c30d1ca26d30aa3dc3c9aa04f0b585ce,
"product_id" => 51,
"line_total"=>99,
"quantity"=>1,
"data"=> array(
"id"=> 51,
"post"=>array(
"ID"=>51,
"post_title"=>"Prodcut four - control",
),
"price"=>99
)
)
);
需要根据“product_id”数组值删除重复值,并根据产品数量增加数量值。
注意:上面的数组也有“唯一键”,因此数组结果中需要任何单个唯一键
Expected Result should be:
$resultproduct = array(
"2e471a22b1b994a7cb3f3a40cee9fba2" => array (
"product" => 6004,
"unique_key" => 3a8a5cb029ee3b92cfc90de23e2329ab,
"product_id" => 51,
"line_total"=>99,
"quantity"=>2,
"data"=> array(
"id"=> 51,
"post"=>array(
"ID"=>51,
"post_title"=>"Prodcut four - control",
),
"price"=>99
)
));
您需要在每个产品上循环,并使用产品id作为新阵列的键。这会按原样添加数量,因此应适用于除1以外的任何数量
$result = [];
foreach ($product as $p)
{
if (isset($result[$p['product_id']]))
{
$result[$p['product_id']]['quantity']+= $p['quantity'];
}
else
{
$result[$p['product_id']] = $p;
}
}
print_r($result);
您需要在每个产品上循环,并使用产品id作为新阵列的键。这会按原样添加数量,因此应适用于除1以外的任何数量
$result = [];
foreach ($product as $p)
{
if (isset($result[$p['product_id']]))
{
$result[$p['product_id']]['quantity']+= $p['quantity'];
}
else
{
$result[$p['product_id']] = $p;
}
}
print_r($result);
我试图使代码易于理解,因此比绝对需要的变量和代码行更多
说明:
1) 需要使用原始产品数组索引之一作为输出表键,例如产品51的“2e471a22b1b994a7cb3f3a40cee9fba2”
2) 它需要将输入productId快速关联到输出键。因此,我使用了一个查找表ProductIdList
,它将productId
匹配到output
键
然后是两阶段查找,以在输出中查找条目并向其添加数量
守则:
// create a product_id => first key table
$productIdList = array();
// output...
$productTotal = array();
foreach ($product as $origIndex => $entry) {
$curProductId = $entry['product_id'];
// check product_id exists in the lookup...
if (isset($productIdList[$curProductId])) { // add to the total...
$productTotalIndex = $productIdList[$curProductId];
$productTotal[$productTotalIndex]['quantity'] += $entry['quantity'];
}
else { // add the entry to the output and the productIdList...
$productIdList[$curProductId] = $origIndex;
$productTotal[$origIndex] = $entry;
}
}
输出:总计数组:
Array
(
[2e471a22b1b994a7cb3f3a40cee9fba2] => Array
(
[product] => 6004
[unique_key] => 3a8a5cb029ee3b92cfc90de23e2329ab
[product_id] => 51
[line_total] => 99
[quantity] => 2
[data] => Array
(
[id] => 51
[post] => Array
(
[ID] => 51
[post_title] => Prodcut four - control
)
[price] => 99
)
)
[test02] => Array
(
[product] => 6664
[unique_key] => c30d1ca26d30aa3dc3c9aa04f0b585ce
[product_id] => 666
[line_total] => 99
[quantity] => 579
[data] => Array
(
[id] => 666
[post] => Array
(
[ID] => 666
[post_title] => Prodcut 666 - control
)
[price] => 99
)
)
)
原始密钥列表的productId:
array (size=2)
51 => string '2e471a22b1b994a7cb3f3a40cee9fba2' (length=32)
666 => string 'test02' (length=6)
我试图使代码易于理解,因此比绝对需要的变量和代码行更多
说明:
1) 需要使用原始产品数组索引之一作为输出表键,例如产品51的“2e471a22b1b994a7cb3f3a40cee9fba2”
2) 它需要将输入productId快速关联到输出键。因此,我使用了一个查找表ProductIdList
,它将productId
匹配到output
键
然后是两阶段查找,以在输出中查找条目并向其添加数量
守则:
// create a product_id => first key table
$productIdList = array();
// output...
$productTotal = array();
foreach ($product as $origIndex => $entry) {
$curProductId = $entry['product_id'];
// check product_id exists in the lookup...
if (isset($productIdList[$curProductId])) { // add to the total...
$productTotalIndex = $productIdList[$curProductId];
$productTotal[$productTotalIndex]['quantity'] += $entry['quantity'];
}
else { // add the entry to the output and the productIdList...
$productIdList[$curProductId] = $origIndex;
$productTotal[$origIndex] = $entry;
}
}
输出:总计数组:
Array
(
[2e471a22b1b994a7cb3f3a40cee9fba2] => Array
(
[product] => 6004
[unique_key] => 3a8a5cb029ee3b92cfc90de23e2329ab
[product_id] => 51
[line_total] => 99
[quantity] => 2
[data] => Array
(
[id] => 51
[post] => Array
(
[ID] => 51
[post_title] => Prodcut four - control
)
[price] => 99
)
)
[test02] => Array
(
[product] => 6664
[unique_key] => c30d1ca26d30aa3dc3c9aa04f0b585ce
[product_id] => 666
[line_total] => 99
[quantity] => 579
[data] => Array
(
[id] => 666
[post] => Array
(
[ID] => 666
[post_title] => Prodcut 666 - control
)
[price] => 99
)
)
)
原始密钥列表的productId:
array (size=2)
51 => string '2e471a22b1b994a7cb3f3a40cee9fba2' (length=32)
666 => string 'test02' (length=6)
foreach中可能存在重复的循环产品数组,然后以您喜欢的方式构建数组结构,如果您找到相同的产品,则递增计数器Idhy Ramkumar,数据来自何处?@Mangeshsathein也需要删除重复的值,所有产品都有唯一的密钥,所以array_unique不起作用。@在上面的数组中,SyedMohamedAladeen所有产品都有唯一的键,所以array_unique不起作用。您给定的链接在数组索引中的所有值都相同,但在我的数组中不起作用,因为所有数组索引都有不同的唯一键。foreach中可能存在循环乘积数组的重复项,然后以您喜欢的方式构建数组结构,如果您找到相同的乘积idHey Ramkumar,则递增计数器,数据从何处来?@mangeshatsathein也需要删除重复值,所有产品都有唯一的密钥,因此array_unique不起作用。@SyedMohamedAladeen在上述数组中,所有产品都有唯一的密钥,因此array_unique不起作用。您给定的链接在数组索引中的所有值都是相同的,但它在我的数组中不起作用,因为所有数组索引都有不同的唯一键。