0

I'm new to PHP, and I'm trying to merge the following two arrays into one array, matching the same CODE value:

Array(1)
(
    [0] => Array
        (
            [0] => CODE11
            [1] => NAME
            [2] => ADDRESS
        )
    [1] => Array
        (
            [0] => CODE23
            [1] => NAME
            [2] => ADDRESS
        )
    [2] => Array
        (
            [0] => CODE25
            [1] => NAME
            [2] => ADDRESS
        )
)

and

Array(2)
(
    [0] => Array
        (
            [0] => CODE43
            [1] => CITY
            [2] => COUNTRY
        )
    [1] => Array
        (
            [0] => CODE25
            [1] => CITY
            [2] => COUNTRY
        )
    [2] => Array
        (
            [0] => CODE89
            [1] => CITY
            [2] => COUNTRY
        )
)

into this new array:

Result
(
    [0] => Array
        (
            [0] => CODE25
            [1] => NAME
            [2] => ADDRESS
            [3] => CITY
            [4] => COUNTRY
        )
)

As you can see, only CODE25 matches on both arrays. How do I fix it?

4
  • Possible duplicate of stackoverflow.com/questions/8561987/php-merge-two-arrays?rq=1 Commented Feb 7, 2014 at 2:51
  • 2
    This doesn't look similar to that question at all. The titles are similar, but that's it. Commented Feb 7, 2014 at 2:57
  • We can't tell you how to fix your code if you don't post it. Commented Feb 7, 2014 at 2:57
  • Why aren't you using associative arrays? Commented Feb 7, 2014 at 2:58

2 Answers 2

2
$hash = array();
// First create an associative array from $array1 using the CODE as the key
foreach ($array1 as $el) {
    $hash[$el[0]] = $el;
}
// Then append the data from $array2 to matching elements
foreach ($array2 as $el) {
    if (isset($hash[$el[0]])) {
        $hash[$el[0]] = array_merge($hash[$el[0]], array_slice($el, 1));
    }
}
// Now find the elements that were matched and return them as an ordinary array
$new_array = array_values(array_filter($hash, function($x) { return count($x) > 3; }));
Sign up to request clarification or add additional context in comments.

3 Comments

+1 for nice and simple algorithm, faster than mine :)
Thank you for your help! but i couldn´t make it work! actually each one of my 2 arrays have 15.000 sub arrays and the algorithm just return 1 result =(
Can you post some sample data with var_export() so I can try running my script with it?
0

We could do this with less code by using native PHP functions, but instead of that and because you're learning, let's try a very basic way:

foreach ($array1 as $key1 => $value1)
{
    foreach ($array2 as $key2 => $value2)
    {
        if ($value1[0] == $value2[0])
            $result[] = array(
                $value1[0],
                $value1[1],
                $value1[2],
                $value2[1],
                $value2[2]
            );
    }
}

Disclaimer: this is not a very efficient algorithm. But for a short amount of data ($array1 and $array2 < 20,000 keys each), it will do the trick with no pain. See Big O notation for more informations about that.

3 Comments

You're copying the CODE into the result twice.
Tried your code..no luck. Actually i´m working with 15.000 arrays and the code i was using was similiar to this: foreach($array1 as $value) { foreach($array2 as $value1) { if($value['0'] == $value1[0]) { do something....; } } } But my pc i7 seems it can´t handle it =(
Great! So you've got a BIG example of how much it matters to find a good algorithm

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.