-1

Existing Record:

array:3 [
  0 => array:2 [
    "groupTaskUuid" => "98790287437987860"
    "purchaseOrderGroupTasks" => "98790287437987893"
  ]
  1 => array:2 [
    "groupTaskUuid" => "98790287437987862"
    "purchaseOrderGroupTasks" => "98790287437987894"
  ]
  2 => array:2 [
    "groupTaskUuid" => "98790287437987861"
    "purchaseOrderGroupTasks" => "98790287437987895"
  ]
]

GroupTaskUuid Array:

array:2 [
  0 => "98790287437987862"
  1 => "98790287437987861"
]

How can I compare two arrays with the "groupTaskUuid" but return "purchaseOrderGroupTasks" value in the result?

Result what I want (that is "purchaseOrderGroupTasks"):

array:2 [
    "0" => "98790287437987894"
    "1" => "98790287437987895"
]
Bryant Tang
  • 251
  • 3
  • 19

3 Answers3

0

I would suggest something like this:

$filtered = array_filter(
                $records, 
                function($el) use($GroupTaskUuid) { 
                    return in_array($el["groupTaskUuid"], $GroupTaskUuid); 
                }
             );

$only_purchase_column = array_column($filtered, "purchaseOrderGroupTasks")
Alberto Sinigaglia
  • 12,097
  • 2
  • 20
  • 48
0

Since you said there would be a lot of rows, you will need to convert GroupTaskUuid array to an associative array where those UUIDs are keys. This way you can do an isset() check in O(1) time on average.

You loop through the dataset, check if that UUID key exists in our map and if it exists, collect it's purchaseOrderGroupTasks in a new result array.

Snippet:

<?php

$data = [
      [
        "groupTaskUuid" => "98790287437987860",
        "purchaseOrderGroupTasks" => "98790287437987893"
      ],
      [
        "groupTaskUuid" => "98790287437987862",
        "purchaseOrderGroupTasks" => "98790287437987894"
      ],
      [
        "groupTaskUuid" => "98790287437987861",
        "purchaseOrderGroupTasks" => "98790287437987895"
      ]
];

$GroupTaskUuid = [
       "98790287437987862",
       "98790287437987861"
    ];

$GroupTaskUuidFlipped = array_flip($GroupTaskUuid); // make UUID values as keys
$purchaseOrderGroupTasks = [];

foreach($data as $value){
    if(isset($GroupTaskUuidFlipped[$value['groupTaskUuid']])){
        $purchaseOrderGroupTasks[] = $value['purchaseOrderGroupTasks'];
    }
}

print_r($purchaseOrderGroupTasks);
nice_dev
  • 17,053
  • 2
  • 21
  • 35
0

You can use

$purchaseOrderGroupTasks = [];
foreach($GroupTaskUuid as $uuid){
    if($key = array_search($uuid, array_column($data, 'groupTaskUuid')) !== false)
        $purchaseOrderGroupTasks[] = $data[$key]['key'];
}
Kongulov
  • 1,156
  • 2
  • 9
  • 19