0

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) ?

5
  • 1
    The only thing you can do here is change your arrays to look like 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). Commented Apr 25, 2012 at 8:05
  • 3
    If all user IDs are unique, you can map array B to arrayB[user_id] => created_time first. Then you can iterate over array A and just lookup the user ID in array B (O(1)). That is around O(2n) instead of O(n^2). Commented Apr 25, 2012 at 8:06
  • Are the userid's in sorted order? Commented Apr 25, 2012 at 8:34
  • the IDs are unique. How to convert array_a( [0] => array('userid' => aaa, 'created_time' => XXXX,), [1] => array('userid' => bbb, 'created_time' => XXXX,)) to array(aaa=>XXXX,bbb=>XXXX)? Commented Apr 25, 2012 at 8:44
  • You have yatta-yatta'ed your sample input which means this is not a clear minimal reproducible example. The format of your datetime value is important. The b array doesn't not contain a bbb userid value. Commented Mar 8, 2024 at 0:46

4 Answers 4

0

You could consider using the userid of each element as the array key. This allows you to look up the correct item in B in O(1) time.

Sign up to request clarification or add additional context in comments.

Comments

0

Sort both arrays by user id then created time. Order is still O(N^2) but the number of comparisons is greatly reduced. However since you're looking for an explicit match on userid, then converting the arrays to array('aaa'=>array(0=>'created_time', 1=>'created_time'...)...) then getting the value of array_intersect(array_a, array_b) will give you all the common user ids.

Comments

0
$b_index = 0;
for ($a_index = 0; $a_index < count($a); $a_index++)
{
    if ($a[$a_index]['userid'] == $b[$b_index]['userid'])
    {
        if ($a[$a_index]['created_time'] >= $b[$b_index]['created_time']) 
            $array[] = $a[$a_index];
        $b_index++;
    }
}

If the userid's are both sorted in the same order, you don't need to compared each userid in a with each userid in b to look for match. This should be less comparisons at least.

Comments

0
foreach($array_a as $arr)  
  $tmp[$arr['userid']] = $arr['created_time']; //rebulding first array

foreach($array_b as $arr)
  if(isset($tmp[$arr['userid']]) && $arr['created_time'] < $tmp[$arr['userid']]){
    //target  
  } 

as first you have to rebuild one of your array to structure suitable for next step, where you will searching for items meeting your condition. this solution should be better than yours because it has much smaller count of loops (2*n instead of n^2)

7 Comments

Well, what about it? Are you asking a question of your own?
ok, I updated my code for those lazy b****** who want everything served on silver plate (sorry for my english, I hope you will understand me ;)
I 'm sorry, but your code will waste a lot of memory to do a grand total of nothing useful. Did you miss the part where created_time plays a role?
@Jon my fault, I am sorry, is this better?
Better, but it still won't compile because it has syntax errors. And not very useful to the OP because there's no explanation at all. Don't forget to fix the notices as well. Writing a good answer isn't very easy, is it?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.