Skip to content

Magento 2 : Get collection of based on field

Here is the collection I have as an output :

Array
(
    [0] => Array
        (
            [item_id] => 1
            [rma_id] => 1
            [product_sku] => 24-MB02
            [subsidiary] => CA
        )

    [1] => Array
        (
            [item_id] => 2
            [rma_id] => 2
            [product_sku] => 24-MB02
            [subsidiary] => DE
        )

    [2] => Array
        (
            [item_id] => 3
            [rma_id] => 3
            [product_sku] => 26-MB02
            [subsidiary] => DE
        )

)

In this, I want to have records with the same SKU but different subsidiaries only.

Desired output,

Array
(
    [0] => Array
        (
            [item_id] => 1
            [rma_id] => 1
            [product_sku] => 24-MB02
            [subsidiary] => CA
        )

    [1] => Array
        (
            [item_id] => 2
            [rma_id] => 2
            [product_sku] => 24-MB02
            [subsidiary] => DE
        )
 )

You see the same product_sku but a different subsidiary.

here is the code.

$rmaCollection = $_objectManager->create('MirasvitRmaModelResourceModelPostCollectionFactory'); //avoid object manager use while development.

$collection = $rmaCollection->create()->load();

print_r($collection->getData());

Any thoughts on this ?