php - 基于特定多个键的唯一多维数组
问题描述
我有一个多维数组,我需要使用基于特定键的唯一数组对其进行排序。item_type_id
具有不同partner_id
和键的键具有相同的值store_id
。我期望的结果是当item_type_id
键具有相同的值并且在具有相同的不同partner_id
键中时store_id
,它应该10017
优先选择partner_id
键上的值。
示例数组
[
0 => [
"partner_id" => "10017"
"store_id" => "1000"
"item_type_id" => "2"
"value" => "58"
"category" => "1"
]
1 => [
"partner_id" => "10017"
"store_id" => "1000"
"item_type_id" => "1"
"value" => "63"
"category" => "1"
]
2 => [
"partner_id" => "0"
"store_id" => "1000"
"item_type_id" => "3"
"value" => "29"
"category" => "1"
]
3 => [
"partner_id" => "0"
"store_id" => "1000"
"item_type_id" => "2"
"value" => "58"
"category" => "1"
]
4 => [
"partner_id" => "0"
"store_id" => "1001"
"item_type_id" => "1"
"value" => "65"
"category" => "1"
]
5 => [
"partner_id" => "0"
"store_id" => "1001"
"item_type_id" => "2"
"value" => "58"
"category" => "1"
]
6 => [
"partner_id" => "0"
"store_id" => "1001"
"item_type_id" => "3"
"value" => "29"
"category" => "1"
]
7 => [
"partner_id" => "0"
"store_id" => "1000"
"item_type_id" => "1"
"value" => "65"
"category" => "1"
]
]
结果
[
1000 => [
0 => [
"partner_id" => "10017"
"store_id" => "1000"
"item_type_id" => "2"
"value" => "58"
"category" => "1"
]
1 => [
"partner_id" => "10017"
"store_id" => "1000"
"item_type_id" => "1"
"value" => "63"
"category" => "1"
]
2 => [
"partner_id" => "0"
"store_id" => "1000"
"item_type_id" => "3"
"value" => "29"
"category" => "1"
]
]
1001 => [
0 => [
"partner_id" => "0"
"store_id" => "1001"
"item_type_id" => "1"
"value" => "65"
"category" => "1"
]
1 => [
"partner_id" => "0"
"store_id" => "1001"
"item_type_id" => "2"
"value" => "58"
"category" => "1"
]
2 => [
"partner_id" => "0"
"store_id" => "1001"
"item_type_id" => "3"
"value" => "29"
"category" => "1"
]
]
]
这是我的脚本
$storeID = [1000,1001];
$createdArray = [];
$previous_item_type_id = "";
$previous_partner_id = "";
foreach($arrays as $array) {
for($i=0; $i<count($storeID); $i++) {
if($array["store_id"] == $storeID[$i]) {
if($array["item_type_id"] != $previous_item_type_id && $array["partner_id"] != $previous_partner_id) {
$createdArray[$storeID[$i]] = [
"partner_id" => $array["partner_id"],
"store_id" => $array["store_id"],
"item_type_id" => $array["item_type_id"],
"value" => $array["value"],
"category" => $array["category"],
];
} else {
$previous_item_type_id = $array["item_type_id"];
$previous_partner_id = $array["partner_id"];
continue;
}
}
}
}
dd($createdArray);
解决方案
您可以使用一个简单的 foreach 循环来遍历您的数组,然后使用子数组键store_id
来获取最终数组的键。将当前值的值推入该最终数组。
另外,要删除重复的item_type_id
,我会编写一个函数来检查该 id 是否已存在于结果中。
例如:
$storeID = [1000,1001];
$array = [
0 => [
"partner_id" => "10017",
"store_id" => "1000",
"item_type_id" => "2",
"value" => "58",
"category" => "1"
],
1 => [
"partner_id" => "10017",
"store_id" => "1000",
"item_type_id" => "1",
"value" => "63",
"category" => "1",
],
2 => [
"partner_id" => "0",
"store_id" => "1000",
"item_type_id" => "3",
"value" => "29",
"category" => "1"
],
3 => [
"partner_id" => "0",
"store_id" => "1000",
"item_type_id" => "2",
"value" => "58",
"category" => "1",
],
4 => [
"partner_id" => "0",
"store_id" => "1001",
"item_type_id" => "1",
"value" => "65",
"category" => "1",
],
5 => [
"partner_id" => "0",
"store_id" => "1001",
"item_type_id" => "2",
"value" => "58",
"category" => "1",
],
6 => [
"partner_id" => "0",
"store_id" => "1001",
"item_type_id" => "3",
"value" => "29",
"category" => "1"
],
7 => [
"partner_id" => "0",
"store_id" => "1000",
"item_type_id" => "1",
"value" => "65",
"category" => "1"
]
];
function ItemIdExists($arr, $itemId)
{
foreach ($arr as $subValue)
{
if ($subValue["item_type_id"] == $itemId)
{
return true;
}
}
return false;
}
$result = array();
foreach ($array as $value)
{
$key = $value["store_id"];
if (in_array($key, $storeID))
{
$ItemIdFound = isset($result[$key]) && ItemIdExists($result[$key], $value["item_type_id"]);
if (!$ItemIdFound)
$result[$key][] = $value;
}
}
var_dump($result);
输出
array(2) {
[1000]=>
array(3) {
[0]=>
array(5) {
["partner_id"]=>
string(5) "10017"
["store_id"]=>
string(4) "1000"
["item_type_id"]=>
string(1) "2"
["value"]=>
string(2) "58"
["category"]=>
string(1) "1"
}
[1]=>
array(5) {
["partner_id"]=>
string(5) "10017"
["store_id"]=>
string(4) "1000"
["item_type_id"]=>
string(1) "1"
["value"]=>
string(2) "63"
["category"]=>
string(1) "1"
}
[2]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1000"
["item_type_id"]=>
string(1) "3"
["value"]=>
string(2) "29"
["category"]=>
string(1) "1"
}
}
[1001]=>
array(3) {
[0]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1001"
["item_type_id"]=>
string(1) "1"
["value"]=>
string(2) "65"
["category"]=>
string(1) "1"
}
[1]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1001"
["item_type_id"]=>
string(1) "2"
["value"]=>
string(2) "58"
["category"]=>
string(1) "1"
}
[2]=>
array(5) {
["partner_id"]=>
string(1) "0"
["store_id"]=>
string(4) "1001"
["item_type_id"]=>
string(1) "3"
["value"]=>
string(2) "29"
["category"]=>
string(1) "1"
}
}
}
推荐阅读
- c# - 嵌套类成员的流利验证
- azure-servicebus-topics - 无法更新 azurefunctions 中的 host.json 文件
- matlab - 表达式识别中的模糊规则插值
- python - 如何从状态栏中隐藏 gui?
- .net - 无法获取未定义或空引用的属性“开始”
- java - 如何使用 START_STICKY 将额外的值传递给服务?
- sql - SQL - 在子查询中选择特定条件
- angular - (Angular)我应该在核心文件夹(作为单例)还是在延迟加载的模块本身中拥有我的 http 服务?
- javascript - 如何获取单个输入字段的值并将每个产品发送到数据库
- javascript - 使用 JavaScript Promise 时如何传递信息