1

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);

2 Answers 2

1

You can use a simple foreach loop to iterate through your array, then use the sub array key store_id to get the key of the final array. push in that final array the value of the current value.

Plus, to remove the duplicated item_type_id, I would write a function that checks if that id already exists in the result.

In example :

$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);

Output

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"
    }
  }
}
Sign up to request clarification or add additional context in comments.

3 Comments

on your result, there is duplicate item_type_id on same store_id. you could see the result that i expected.
duplicate partner_id yes, but the store_id and item_type_id are different.
Ah you want to keep the distinct pair partner_id and item_type_id ? editing
0

If I understand the requirements, the desired result will:

  • group on store_id values and
  • when item_type_id values collide, the row with a non-zero partner_id value should overwrite a row with a zero partner_id value

Using temporary keys to construct a deeper lookup structure will allow greater efficiency that searching in a loop because key lookups with isset() will always outperform value lookups (with any technique -- even an early return).

After all of the grouping and filtering is done in the loop, flatten the data relating to each store into an associative array of indexed arrays.

Code: (Demo)

$result = [];
foreach ($array as $row) {
    if ($row['partner_id'] || !isset($result[$row['store_id']][$row['item_type_id']])) {
        $result[$row['store_id']][$row['item_type_id']][$row['partner_id']] = $row;
    }
}

var_export(array_map(function($row) { return array_merge(...$row); }, $result));

The snippet above only adds a row into the restructured array if the partner_id is not zero or if the combination of store_id-item_type_id-partner_id has not been encountered before.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.