检查 array2 中 array1 的值

发布于 2024-11-25 20:09:22 字数 1101 浏览 0 评论 0原文

在 php 中有以下两个不同的数组:

Array1
(
    [0] => Array
        (
            [id] => 1
            [name] => William
        )

    [1] => Array
        (
            [id] => 2
            [name] => Bob
        )

    [2] => Array
        (
            [id] => 3
            [name] => Michael
        )

)
Array2
(
    [0] => Array
        (
            [id] => 11128
            [name] => John
        )

    [1] => Array
        (
            [id] => 11127
            [name] => Adam
        )

    [2] => Array
        (
            [id] => 11126
            [name] => Andrew
        )

    [3] => Array
        (
            [id] => 11125
            [name] => William
        )

    [4] => Array
        (
            [id] => 11124
            [name] => Bob
        )

)

我能以某种方式找到匹配的 name 吗,这样输出将如下所示:

Array1 (1, id:2) matches Array2 (4, id:11124) with name: Bob
Array1 (0, id:1) matches Array2 (3, id:11125) with name: William

如果不清楚,抱歉

Having the following two different arrays in php:

Array1
(
    [0] => Array
        (
            [id] => 1
            [name] => William
        )

    [1] => Array
        (
            [id] => 2
            [name] => Bob
        )

    [2] => Array
        (
            [id] => 3
            [name] => Michael
        )

)
Array2
(
    [0] => Array
        (
            [id] => 11128
            [name] => John
        )

    [1] => Array
        (
            [id] => 11127
            [name] => Adam
        )

    [2] => Array
        (
            [id] => 11126
            [name] => Andrew
        )

    [3] => Array
        (
            [id] => 11125
            [name] => William
        )

    [4] => Array
        (
            [id] => 11124
            [name] => Bob
        )

)

Can I somehow find matching names so the output will be like:

Array1 (1, id:2) matches Array2 (4, id:11124) with name: Bob
Array1 (0, id:1) matches Array2 (3, id:11125) with name: William

sorry if it's unclear

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

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

发布评论

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

评论(2

a√萤火虫的光℡ 2024-12-02 20:09:22

您始终可以尝试一个简单的 foreach 循环:

<?php
foreach($array1 as $key1=>$value1) {
    foreach($array2 as $key2=>$value2) {
        if($value1['name'] === $value2['name']) {
            sprintf('Array1 (%d, id:%d) matches Array2 (%d, id:%d) with name: %s', $key1, $value1['id'], $key2, $value2['id'], $value1['name']);
        }
    }
}
?>

You can always try a simple foreach loop:

<?php
foreach($array1 as $key1=>$value1) {
    foreach($array2 as $key2=>$value2) {
        if($value1['name'] === $value2['name']) {
            sprintf('Array1 (%d, id:%d) matches Array2 (%d, id:%d) with name: %s', $key1, $value1['id'], $key2, $value2['id'], $value1['name']);
        }
    }
}
?>
寂寞陪衬 2024-12-02 20:09:22
foreach($array1 as $i=>$x){
  foreach($array2 as $k=>$y){
    if($x['name'] == $y['name']){
      echo "Array1 ($i, id:{$x['id']}) matches Array2 ($k, id:{$y['id']}) with name: {$x['name']}\n";
      break;
    }
  }
}
foreach($array1 as $i=>$x){
  foreach($array2 as $k=>$y){
    if($x['name'] == $y['name']){
      echo "Array1 ($i, id:{$x['id']}) matches Array2 ($k, id:{$y['id']}) with name: {$x['name']}\n";
      break;
    }
  }
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文