首页 > 解决方案 > 基于特定多个键的唯一多维数组

问题描述

我有一个多维数组,我需要使用基于特定键的唯一数组对其进行排序。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);

标签: phparrays

解决方案


您可以使用一个简单的 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"
    }
  }
}

推荐阅读