2

i have 2 arryas and i want them to intersect and store the finding matches into third array with values from first array and second array. the first array looks like this:

Array
(
    [0] => Array
        (
            [0] => 45
            [1] => 10640
            [2] => 1041-0567041700116
        )

    [1] => Array
        (
            [0] => 46
            [1] => 10640
            [2] => 1041-0567041700318
        )
    [2] => Array
        (
            [0] => 207
            [1] => 10645
            [2] => 03320103000052
        )

and the second array:

Array
(
    [0] => Array
        (
            [0] => 03320103000052
            [1] => 0
        )

    [1] => Array
        (
            [0] => 10013800805001
            [1] => 12
        )

    [2] => Array
        (
            [0] => 1090-0360141758201
            [1] => 3
        )

the out put should be:

Array
(
        [0] => Array
            (
                [0] => 207                     =>value from first array
                [1] => 10645                   =>value from first array
                [2] => 03320103000052          =>value from first and second array (this is what i need to compare)
                [3] => 0                       =>value from second array
            )

this is similar to this post

but i have problems to store data into multidimensional array

thanks in forward for any suggestions and help

6
  • I don't understand how 207, 10645, 03320103000052 and 0 are grouped together. how do they intersect? Commented Mar 27, 2014 at 6:01
  • i intersect 2 arrays. if matches found (matching value is 03320103000052) and then i want to take the other values from first and second array into new array Commented Mar 27, 2014 at 6:05
  • This seems somewhat overcomplicated to be practically applied ... how are you using all of this? Commented Mar 27, 2014 at 6:06
  • the first array is from database and the second array is from another database as csv export. when i intersect into new array i would be able to update the new data in first database Commented Mar 27, 2014 at 6:09
  • 1
    And you dropped all the column names? Those would be very useful in this case, otherwise the merge will be horribly expensive. Commented Mar 27, 2014 at 6:16

1 Answer 1

1

You can do this with only two foreach loops and one if statement:

$combined = array();
foreach ($array1 as $a) {
    foreach ($array2 as $b) {
        if ($a[2] == $b[0]) {
            $combined[] = array($a[0], $a[1], $a[2], $b[1]);
        }
    }
}

The following is the test I set up to try this:

<?php
$array1 = array();
$array1[] = array('45', '10640', '1041-0567041700116');
$array1[] = array('46', '10640', '1041-0567041700318');
$array1[] = array('207', '10645', '03320103000052');

$array2 = array();
$array2[] = array('03320103000052', '0');
$array2[] = array('10013800805001', '12');
$array2[] = array('1090-0360141758201', '3');


$combined = array();
foreach ($array1 as $a) {
    foreach ($array2 as $b) {
        if ($a[2] == $b[0]) {
            $combined[] = array($a[0], $a[1], $a[2], $b[1]);
        }
    }
}

print_r($combined);
?>
Sign up to request clarification or add additional context in comments.

8 Comments

your answer fails if the sub-array in $b has more or less than 2 values
also what if a[1] == b[1] ? you cannot simply say that only a[2] will only be equal to b[1]
my question is anwsered. guys, you rock. because both codes are working in my case i cannot give to both the check mark and i am very dissapointed about this so i will leave it blank
Why the mark down if this solves the users programming question? The user didn't ask for a 4 loop solution to cover a bunch of different might happen scenarios.
comment to krishna: a[2] will be always the same as b[0] and the b[1] doesnt matter. the comparison of a[1] == b[1] also doesnt matter. only the comparison of $a[2] == $b[0] does matter and the rest of data needed to be combined
|

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.