数组操作

发布于 2024-11-03 04:36:00 字数 3485 浏览 0 评论 0原文

我有一个两列数组(array1),对于该数组的每一行,我需要将第二列中的值与另一个数组(array1)每行中的列值进行比较,当它们相等时我想附加另一个列值(从 array2)到第一个数组。

英语:

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

两个阵列的屏幕转储

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] => [email protected] 
        [6] => jsmith 
        [7] => 12345 
        [8] => 1 
        [9] => no 
    ) 
    [1] => Array ( 
        [0] => 2 
        [1] => 2 
        [2] => 3 
        [3] => James 
        [4] => Beard 
        [5] => [email protected] 
        [6] => jbeard03 
        [7] => keeper 
        [8] => 1 
        [9] => no 
    ) 
    [2] => Array ( 
        [0] => 3 
        [1] => 2 
        [2] => 
        [3] => Peter 
        [4] => Allen 
        [5] => [email protected] 
        [6] => pallen 
        [7] => pallen 
        [8] => 1 
        [9] => no 
    ) 
    [3] => Array ( 
        [0] => 7 
        [1] => 2 
        [2] => 1 
        [3] => Joe 
        [4] => Blow 
        [5] => [email protected] 
        [6] => jblow 
        [7] => blow123 
        [8] => 5 
        [9] => yes 
    ) 
    [4] => Array ( 
        [0] => 8 
        [1] => 2 
        [2] => 1 
        [3] => John 
        [4] => Smith 
        [5] => [email protected] 
        [6] => jnsmith 
        [7] => jsmith123 
        [8] => 4 
        [9] => yes 
    ) 
    [5] => Array ( 
        [0] => 9 
        [1] => 2 
        [2] => 10 
        [3] => Frank 
        [4] => Smith 
        [5] => [email protected] 
        [6] => fsmith 
        [7] => fsmith123 
        [8] => 4 
        [9] => yes 
    ) 
    [6] => Array ( 
        [0] => 10 
        [1] => 2 
        [2] => 2 
        [3] => Loretta 
        [4] => Beard 
        [5] => [email protected] 
        [6] => lbeard 
        [7] => lbeard123 
        [8] => 1 
        [9] => no 
    ) 
) 

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] => [email protected] 
        [6] => jsmith 
        [7] => 12345 
        [8] => 1 
        [9] => no 
    ) 
    [1] => Array ( 
        [0] => 2 
        [1] => 2 
        [2] => 3 
        [3] => James 
        [4] => Beard 
        [5] => [email protected] 
        [6] => jbeard03 
        [7] => keeper 
        [8] => 1 
        [9] => no 
    ) 
    [2] => Array ( 
        [0] => 3 
        [1] => 2 
        [2] => 
        [3] => Peter 
        [4] => Allen 
        [5] => [email protected] 
        [6] => pallen 
        [7] => pallen 
        [8] => 1 
        [9] => no 
    ) 
    [3] => Array ( 
        [0] => 7 
        [1] => 2 
        [2] => 1 
        [3] => Joe 
        [4] => Blow 
        [5] => [email protected] 
        [6] => jblow 
        [7] => blow123 
        [8] => 5 
        [9] => yes 
    ) 
    [4] => Array ( 
        [0] => 8 
        [1] => 2 
        [2] => 1 
        [3] => John 
        [4] => Smith 
        [5] => [email protected] 
        [6] => jnsmith 
        [7] => jsmith123 
        [8] => 4 
        [9] => yes 
    ) 
    [5] => Array ( 
        [0] => 9 
        [1] => 2 
        [2] => 10 
        [3] => Frank 
        [4] => Smith 
        [5] => [email protected] 
        [6] => fsmith 
        [7] => fsmith123 
        [8] => 4 
        [9] => yes 
    ) 
    [6] => Array ( 
        [0] => 10 
        [1] => 2 
        [2] => 2 
        [3] => Loretta 
        [4] => Beard 
        [5] => [email protected] 
        [6] => lbeard 
        [7] => lbeard123 
        [8] => 1 
        [9] => no 
    ) 
) 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

茶色山野 2024-11-10 04:36:00

这有效吗?我不确定我是否完全清楚您在寻找什么。

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

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];
        }
    }
}
夏至、离别 2024-11-10 04:36:00
foreach ($array1 as &$a1) {
    foreach ($array2 as $a2) {
        if ($a1[1] == $a2[0]) {
            $a1[] = $a2[2];
            continue 2;
        }
    }
}

顺便说一句,您应该尝试使用实际有意义的显式键,例如 $a1['id'] 而不是 $a1[1]。当您两个月后再次查看代码时,您会感谢自己。

因为我威胁要这样做:

顺便说一句,你应该尝试使用实际上有意义的显式密钥,例如 $a1['id'] 而不是 $a1[1],当你在两个月后再次查看代码时,你会感谢自己;-P

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文