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'],
];
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]);
}
}