I have a multidimensional array which I need to be sorted with unique array based on specific keys. There are same values on item_type_id keys with different partner_id and store_id keys. The result that I expect is when the item_type_id keys have same value and in different partner_id keys with same store_id, it should prefer 10017 value first on partner_id keys.
Example 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"
]
]
Results
[
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"
]
]
]
Here is my script
$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);