merging overlapping array in php

codemania

I have two array like below:

$array1 = array(
               [0]=>array([0]=>a_a [1]=>aa)
               [1]=>array([0]=>b_b [1]=>bb) 
               [3]=>array([0]=>c_c [1]=>cc) 
               )


$array2 = array(
               [0]=>array([0]=>aa [1]=>AA)
               [1]=>array([0]=>bb [1]=>BB) 
               [3]=>array([0]=>cc [1]=>CC) 
               )

what i would like to merge or overlap to output like below:

$result = array(
               [0]=>array([0]=>a_a [1]=>AA)
               [1]=>array([0]=>b_b [1]=>BB) 
               [3]=>array([0]=>c_c [1]=>CC) 
               )

either output like below:

$result = array(
               [0]=>array([0]=>a_a [1]=>aa [2]=>AA)
               [1]=>array([0]=>b_b [1]=>bb [2]=>AA) 
               [3]=>array([0]=>c_c [1]=>cc [2]=>AA) 
               )

how i do this thing what is the best way any suggestion.

Kevin

I don;t know which is the best way but you could do this with two loops. Example:

$result = array();
foreach($array1 as $val1) {
    foreach($array2 as $val2) {
        if($val1[1] == $val2[0]) {
            $result[] = array($val1[0], $val1[1], $val2[1]);
        }
    }
}

echo '<pre>';
print_r($result);

For the first result, its easily modifiable:

$result[] = array($val1[0], $val2[1]);

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related