Here's the two array to be compared.
array_a(
[0] => array('userid' => aaa, 'created_time' => XXXX,),
[1] => array('userid' => bbb, 'created_time' => XXXX,),
[2] => array('userid' => ccc, 'created_time' => XXXX,)
)
array_b(
[0] => array('userid' => aaa, 'created_time' => XXXX,),
[1] => array('userid' => ccc, 'created_time' => XXXX,),
[2] => array('userid' => ddd, 'created_time' => XXXX,)
)
I wanna retrieve all the element that match the following conditions: array_a's userid is in array_b and array_a's created_time is newer than array_b's
I use the following code to do this,but it will take a long time if the array is huge.
for array_a{
for array_b{
if (a[user_id] = b[user_id] && a[created_time] > b[created_time]) {
//target got
}
}
}
Is there any way to do this logic efficiently?
Thanks for answering. the IDs are unique. How to convert array_a( [0] => array('userid' => aaa, 'created_time' => XXXX,), [1] => array('userid' => bbb, 'created_time' => XXXX,), )
to the form array(aaa=>XXXX,bbb=>XXXX) ?
array('aaa' => XXXX, 'bbb' => XXXX). Especially if you can do it before the data gets in this format (which is highly unsuitable for the job).arrayB[user_id] => created_timefirst. Then you can iterate over array A and just lookup the user ID in array B (O(1)). That is aroundO(2n)instead ofO(n^2).bbbuserid value.