开发者

array manipulation

开发者 https://www.devze.com 2023-02-27 22:42 出处:网络
I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append

I have a two column array (array1), for each row of that array I need to compare the value in the 2nd column with a column value in each row of another array(array1) , when they equal I want to append another column value (from array2) to the first array.

in english:

if array1[x][1] = array2[y][0]
then array1[x][2] = array2[y][2]

screen dumps of both arrays

array1 ( 
    [1] => Array ( 
        [0] => 1 [1] => 2 
    ) 
    [2] => Array ( 
        [0] => 2 [1] => 3 
    ) 
    [3] => Array ( 
        [0] => 3 [1] => 
    ) [7] => Array ( 
        [0] => 7 [1] => 1 
    ) 
    [8] => Array ( 
        [0] => 8 [1] => 1 
    ) 
    [9] => Array (
        [0] => 9 [1] => 10 
    ) 
    [10] => Array (
        [0] => 10 [1] => 2 
    ) 
)


array2 ( 
    [0] => Array ( 
        [0] => 1 
        [1] => 2 
        [2] => 2 
        [3] => Jane 
        [4] => Smith 
        [5] => jsmith@internet.com 
        [6] => jsmith 
        [7] => 12345 
        [8] => 1 
        [9] => no 
    ) 
    [1] => Array ( 
        [0] => 2 
        [1] => 2 
        [2] => 3 
        [3] => James 
        [4] => Beard 
        [5] => jasb@bellsouth.net 
        [6] => jbeard03 
        [7] => keeper 
        [8] => 1 
        [9] => no 
    ) 
    [2] => Array ( 
        [0] => 3 
        [1] => 2 
        [2] => 
        [3] => Peter 
        [4] => Allen 
        [5] => pallen@rfgg.com 
        [6] => pallen 
        [7] => pallen 
        [8] => 1 
        [9] => no 
    ) 
    [3] => Array ( 
        [0] => 7 
        [1] => 2 
        [2] => 1 
        [3] => Joe 
        [4] => Blow 
        [5] => jasb@be开发者_JAVA技巧llsouth.net 
        [6] => jblow 
        [7] => blow123 
        [8] => 5 
        [9] => yes 
    ) 
    [4] => Array ( 
        [0] => 8 
        [1] => 2 
        [2] => 1 
        [3] => John 
        [4] => Smith 
        [5] => logtest@bellsouth.net 
        [6] => jnsmith 
        [7] => jsmith123 
        [8] => 4 
        [9] => yes 
    ) 
    [5] => Array ( 
        [0] => 9 
        [1] => 2 
        [2] => 10 
        [3] => Frank 
        [4] => Smith 
        [5] => pallen@test.com 
        [6] => fsmith 
        [7] => fsmith123 
        [8] => 4 
        [9] => yes 
    ) 
    [6] => Array ( 
        [0] => 10 
        [1] => 2 
        [2] => 2 
        [3] => Loretta 
        [4] => Beard 
        [5] => lbeard@me.net 
        [6] => lbeard 
        [7] => lbeard123 
        [8] => 1 
        [9] => no 
    ) 
) 


Does this work? I'm not sure I'm entirely clear on what you're looking for.

foreach($array1 as $x => $xarray) {
    foreach($array2 as $y => $yarray) {
        if($xarray[1] == $yarray[0]) {
            $array1[$x][2] = $array[$y][2];
        }
    }
}


foreach ($array1 as &$a1) {
    foreach ($array2 as $a2) {
        if ($a1[1] == $a2[0]) {
            $a1[] = $a2[2];
            continue 2;
        }
    }
}

BTW, you should try to use explicit keys that actually mean something, e.g. $a1['id'] instead of $a1[1]. You'll thank yourself when you look at the code again two months down the road.

And because I threatened to do so:

btwyoushouldtrytouseexplicitkeysthatactuallymeansomethingeg$a1['id']insteadof$a1[1]youllthankyourselfwhenyoulookatthecodeagaintwomonthsdowntheroad ;-P

0

精彩评论

暂无评论...
验证码 换一张
取 消