NineCattoRules
NineCattoRules

Reputation: 2428

Modify/Filter two 2d arrays based on the existence of related data between them

I have two different multidimensional arrays as follows:

$first = [
    ['timestamp' => '7/10/2018 15:24:06', 'username' => 'giakhang', 'status' => null],
    ['timestamp' => '8/10/2018 5:11:25', 'username' => 'haophan', 'status' => null],
    ['timestamp' => '8/10/2018 6:38:18', 'username' => 'TTQ1504', 'status' => null],
    ['timestamp' => '08/10/2018 7:04:20', 'username' => 'btcgainer24724', 'status' => null],
];
$second = [
    ['timestamp' => '8/10/2018 5:10:06', 'username' => 'giakhang'],
    ['timestamp' => '8/10/2018 5:13:25', 'username' => 'btcgainer24724'],
    ['timestamp' => '8/10/2018 6:44:18', 'username' => 'anggie88'],
    ['timestamp' => '08/10/2018 7:55:20', 'username' => 'ZeusTrade'],
];

For each same username between 1st_array and 2nd_array I wish to change the status in the 1st_array and unset from the 2nd_array objects not intersected between the two.

Desired results:

$first = [
    ['timestamp' => '7/10/2018 15:24:06', 'username' => 'giakhang', 'status' => 'Yes'],
    ['timestamp' => '8/10/2018 5:11:25', 'username' => 'haophan', 'status' => 'No'],
    ['timestamp' => '8/10/2018 6:38:18', 'username' => 'TTQ1504', 'status' => 'No'],
    ['timestamp' => '08/10/2018 7:04:20', 'username' => 'btcgainer24724', 'status' => 'Yes'],
];
$second = [
    ['timestamp' => '8/10/2018 5:10:06', 'username' => 'giakhang'],
    ['timestamp' => '8/10/2018 5:13:25', 'username' => 'btcgainer24724'],
];

Upvotes: 0

Views: 71

Answers (3)

mickmackusa
mickmackusa

Reputation: 47894

I recommend modifying the first and second arrays by leveraging reference variables instead of making nested loops.

Since no filtering is done on the first array, just declare the status elements as modifiable, declare a username-keyed reference for later use, and set the default status value to No.

Then loop over the second array and either change the status value in the first array to Yes or remove the row from the second array.

This is a very clean, linear, performant approach.

Code: (Demo)

foreach ($first as ['username' => $username, 'status' => &$ref[$username]]) {
    $ref[$username] = 'No';
}

foreach ($second as $i => ['username' => $username]) {
    if (isset($ref[$username])) {
        $ref[$username] = 'Yes';
    } else {
        unset($second[$i]);
    }
}
var_dump($first, $second);

Upvotes: 0

andcl
andcl

Reputation: 3548

Probably there could be a cleaner solution, but here is my attempt:

foreach ($1stArray as $1st) {
    foreach ($2ndArray as $2nd) {
        if ($1st['username'] == $2nd['username']) 
            $1st['status'] = 'Yes';
    }
    if (!$1st['status']) {
        $1st['status'] = 'No';
        unset($2ndArray[array_search($1st['username'], $1st)]);
    }
}

// Finally, we sanitize the 2ndArray indexes
$2ndArray = array_values($2ndArray);

Upvotes: 0

FatemehNB
FatemehNB

Reputation: 271

Try this one :

foreach ($second_array as $key => $value) {
        $exist = false;
        foreach ($first_array as $key2 => $value2) {
            if ($value['username'] == $value2['username']) {
                $exist = true;
                $first_array[$key2]['status'] = 'Yes';
                break;
            } elseif (!$first_array[$key2]['status']) {
                $first_array[$key2]['status'] = 'No';
            }
        }

        if (!$exist) {

            unset($second_array[$key]);
        }
    }

Upvotes: 1

Related Questions