3

I have two big arrays with around 1000 keys and every day growing. How can I merge this arrays into one by value in array1[]['uid'] and array2[][7]

Please note that correct value could be for example array1[45]['uid'] and array2[155][7] or array1[444]['uid'] and array2[666][7] etc.

array1
    0 => 
    array
      'login' => string '104' (length=5)
      'uid' => string '1363861889.100' (length=14)
    1 => 
    array
      'login' => string '131' (length=5)
      'uid' => string '1363863722.126' (length=14)
    etc...

and the other one

array2
     0 => 
    array
      0 => string '2013' (length=4)
      1 => string '03' (length=2)
      2 => string '25' (length=2)
      3 => string '15' (length=2)
      4 => string '39' (length=2)
      5 => string '49' (length=2)
      6 => string 'anonymous' (length=9)
      7 => string '1363863722.126' (length=19)
    1 => 
    array
      0 => string '2013' (length=4)
      1 => string '03' (length=2)
      2 => string '25' (length=2)
      3 => string '12' (length=2)
      4 => string '39' (length=2)
      5 => string '42' (length=2)
      6 => string 'anonymous' (length=9)
      7 => string '1363861889.100' (length=19)
    etc...

array1[0]['uid'] has the same value herearray2[1][7] I would like get:

 array
      0 => string '2013' (length=4)
      1 => string '03' (length=2)
      2 => string '25' (length=2)
      3 => string '12' (length=2)
      4 => string '39' (length=2)
      5 => string '42' (length=2)
      6 => string 'anonymous' (length=9)
      7 => string '1363861889.100' (length=19)
      'login' => string '104' (length=5)
      'uid' => string '1363861889.100' (length=14)
1

2 Answers 2

2
$array1_inverted = array();
foreach ($array1 as $subarray) {
  $array1_inverted[$subarray['uid']] = $subarray;
}
foreach ($array2 as &$element) {
  $array1_element = $array1_inverted[$element[7]];
  $element['uid'] = $array1_element['uid'];
  $element['login'] = $array1_element['login'];
}

DEMO

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

6 Comments

It doesn't work as aspected. Keys uid and login are added but with no value
Fixed -- $array1[$element[7]] was supposed to be $array1_inverted[$element[7]].
Great! :-) Nice coding skills you got there! I deleted my answer because I saw your one was much better.
After testing it $array1_element is the index value of $array1. How can it returns any value in $element['uid'] = $array1_element['uid'];. I think it should be $element['uid'] = $array1[$array1_element]['uid']; Please suggest.
@SumitRaghav You're right, it doesn't work as I wrote. I've changed the first loop so that $array1_inverted holds the subarrays rather than the indexes. I've tested this and it works.
|
1
foreach($array1 as $value)
{
  foreach($array2 as $value1)
  {
    if($value['uid'] == $value1[7])
  {
     $value1['uid'] = $value['uid'];
     $value1['login'] = $value['login'];
  }
 }
}

Try it meanwhile I wil think of some better alternative

1 Comment

See my answer for a non-Nsquared implementation.

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.