如果一个多维数组中的子数组与另一个多维数组不同,则覆盖该子数组

2024-05-14

我坚持这个问题,真的不知道如何解决。 我有两个多维数组,需要将第二个数组中的每个“entry_id”与第一个数组进行匹配。然后需要检查第二个数组中的每个“file_no”是否在数据库(第一个数组)中,并且“status”是否与第一个数组匹配。如果“状态”不同,请使用字符串(例如更新后的值)更新第二个数组,如下所示:

...
[status] => Array
                    (
                        [0] => abc
                        [1] => defghijk - "updated value"
                    )    

所以我有数据库中的第一个数组:

Array
(
    [0] => Array
        (
            [entry_id] => 1
            [file_no] => KSBR 40 INS 3674 / 2014
            [status] => abc
        )

    [1] => Array
        (
            [entry_id] => 9
            [file_no] => KSUL 77 INS 18898 / 2013
            [status] => abc
        )

    [2] => Array
        (
            [entry_id] => 9
            [file_no] => KSUL 77 INS 21218 / 2013
            [status] => defg
        )

)

从脚本生成的第二个数组:

Array
(
    [0] => Array
        (
            [entry_id] => 1
            [id] => 500910/098
            [fullname] => Milan Vrtal
            [type] => person
            [file_no] => Array
                (
                    [0] => KSBR 26 INS 37146 / 2013
                    [1] => KSBR 40 INS 3674 / 2014
                )

            [status] => Array
                (
                    [0] => status1
                    [1] => status2
                )    
        )

    [1] => Array
        (
            [entry_id] => 2
            [id] => 46900217
            [fullname] => ENTEC a.s.
            [type] => company
            [file_no] => Array
                (
                    [0] => KSBR 28 INS 1232 / 2013
                )

            [status] => Array
                (
                    [0] => qwer
                )
        )

    [2] => Array
        (
            [entry_id] => 9
            [fullname] => Blanka Kořínková
            [type] => person
            [file_no] => Array
                (
                    [0] => KSUL 77 INS 18898 / 2013
                    [1] => KSUL 77 INS 21218 / 2013
                )

            [status] => Array
                (
                    [0] => abc
                    [1] => defghijk
                )    
        )
)

感谢您的每一条评论,对英语表示抱歉:)


这是通过创建一个临时数组来搜索。当数组很大时,这将使用相当多的内存,但会导致更快的执行时间......

$tmparr = array();
foreach($arr1 as $arr1_val)
{
  //put an new element in $temparr with key 'entry_id' and an array as value
  if (!isset($tmparr[$arr1_val['entry_id']]))
    $tmparr[$arr1_val['entry_id']] = array();

  //add the status to the array
  $tmparr[$arr1_val['entry_id']][] = $arr1_val['status'];
}
/*
$tmparr = Array
(
    [1] => Array
        (
            [0] => abc
        )

    [9] => Array
        (
            [0] => abc
            [1] => defg
        )

)
*/

//arr2_val by reference so that we can change it
foreach($arr2 as &$arr2_val)
{
  //get the current entry_id
  $entry_id = $arr2_val['entry_id'];
  //see if this entry_id was in the first array, and if so...
  if (isset($tmparr[$entry_id]))
  {
    //change the status to both the original status and the status of the first array
    $arr2_val['status'] = array_merge($arr2_val['status'],$tmparr[$entry_id]);
  }
}

print_r($arr2);

Output:

Array
(
    [0] => Array
        (
            [entry_id] => 1
            [id] => 500910/098
            [fullname] => Milan Vrtal
            [type] => person
            [file_no] => Array
                (
                    [0] => KSBR 26 INS 37146 / 2013
                    [1] => KSBR 40 INS 3674 / 2014
                )

            [status] => Array
                (
                    [0] => status1
                    [1] => status2
                    [2] => abc
                )

        )

    [1] => Array
        (
            [entry_id] => 2
            [id] => 46900217
            [fullname] => ENTEC a.s.
            [type] => company
            [file_no] => Array
                (
                    [0] => KSBR 28 INS 1232 / 2013
                )

            [status] => Array
                (
                    [0] => qwer
                )

        )

    [2] => Array
        (
            [entry_id] => 9
            [fullname] => Blanka Kořínková
            [type] => person
            [file_no] => Array
                (
                    [0] => KSUL 77 INS 18898 / 2013
                    [1] => KSUL 77 INS 21218 / 2013
                )

            [status] => Array
                (
                    [0] => abc
                    [1] => defghijk
                    [2] => abc
                    [3] => defg
                )

        )

)

编辑:这是可能的,没有临时数组,但有一个循环中的循环。这将比第一个慢,但消耗的内存更少:

//arr2_val by reference so that we can change it
foreach($arr2 as &$arr2_val)
{   
  //get the current entry_id
  $entry_id = $arr2_val['entry_id'];
  //search for the correct row in the first array
  foreach($arr1 as $arr1_val)
  {   
    if ($arr1_val['entry_id'] == $arr2_val['entry_id'])
    {   
      $arr2_val['status'][] = $arr1_val['status'];
      //a continue should be added here to make it faster...
    }
  }   
}   

print_r($arr2);
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)

如果一个多维数组中的子数组与另一个多维数组不同,则覆盖该子数组 的相关文章

  • PHP 或 WAMP 不确定是什么

    我已经安装了 WAMP 服务器 2 0 PHP 5 4 3 安装WAMP后我已经重新启动了所有服务并且可以打开 phpinfo 显示良好 phpmyadmin 它也显示得很好 我可以使用数据库 然而 当在 Chrome 中运行简单的 php
  • 如何将 HTML 转换为 Markdown?

    我有一个类似 stackoverflow 的网站 有一个文本区域 人们可以在其中写答案 我用这个 PHP 库 http parsedown org 转换降价 我的意思是我使用该函数来转换 italic to i italic i inclu
  • 创建动态多维对象/数组

    我正在尝试使用 JS 创建一个多维数组 以便我可以通过 Ajax 调用 PHP 来发布一些数据 这可能很简单 但我对 JS 的了解很少关于这个具体的事情 这是带有代码的 JSFiddle http jsfiddle net k5Q3p 我想
  • 阻止注销页面后的后退按钮

    我有 php 注销页面 当用户单击注销链接时 请参阅此页面并重定向到索引页面 但是当单击后退按钮时 我会看到带有用户数据的上一页 当然 当我刷新页面时 我看不到以前的页面和数据 我在单击注销并单击后退按钮后检查了其他代码 drupal 但我
  • 负载平衡集群中的 PHP 会话 - 如何?

    好的 我得到了这个完全罕见的负载平衡 PHP 网站的独特场景 令人遗憾的是 它过去没有进行负载平衡 现在我们开始遇到问题 目前唯一的问题是 PHP 会话 当然 一开始没有人想到这个问题 因此 PHP 会话配置保留为默认值 因此 两台服务器都
  • 使用先前的反向引用作为命名捕获组的名称

    有没有办法使用对先前捕获组的反向引用作为捕获组的名称命名捕获组 这可能不可能 如果不可能 那么这就是一个有效的答案 下列 data description some description preg match data matches p
  • 如何在 PHP 中使用 cURL 发出同时包含 GET 和 POST 参数的请求?

    其他人已经问过如何从 perl java bash 等执行此操作 但我需要在 PHP 中执行此操作 并且我没有看到任何已提出的专门与 PHP 相关的问题 或包含 PHP 的答案 My code ch curl init url curl s
  • PHP Solr PECL 扩展安装

    我已经使用命令安装了 pecl solr pecl install solr 和梨使用 wget http pear php net go pear phar php go pear phar 重启Apache后 我仍然收到错误 Fatal
  • .push() 将多个对象放入 JavaScript 数组中返回“未定义”

    当我将项目添加到beats数组然后console log用户时 我得到了数组中正确的项目数 但是当我检查 length 时 我总是得到 1 尝试调用索引总是会给我 未定义 如下所示 Tom beats 1 我想我错过了一些明显的东西 但这让
  • Laravel 5 中的自定义验证器

    我正在将 Laravel 应用程序从 4 升级到 5 但是 我有一个自定义验证器 但无法运行 在L4中 我做了一个验证器 php文件并将其包含在全局 php using require app path validators php 我尝试
  • 语法错误,第 288 行出现意外的“endif”(T_ENDIF)[关闭]

    Closed 这个问题是无法重现或由拼写错误引起 help closed questions 目前不接受答案 我一直在离线处理我的 WordPress 网站的此代码错误 解析错误 语法错误 homez 541 photoher marie
  • 3D 数组到 3D std::vector

    我在代码函数中用 3D std vector 替换了 3D 数组 它进入了无限循环 你能给我一个提示吗 我真的需要使用向量而不是数组 谢谢 我最初的代码是 arr is a 3D array of a sudoku table the 3
  • 使用 PHP 的 MySQL 连接字符串

    我正在尝试通过本地计算机连接到托管在我的服务器上的数据库 我的服务器有cPanel 11 它是一个典型的共享服务器 由CentOS提供支持 安装了PHP和MySQL 准确地说 我在同一台服务器上持有经销商帐户 我想在不同帐户或域之间访问数据
  • 按第一列排序二维数组,然后按第二列排序

    int arrs 1 100 11 22 1 11 2 12 Arrays sort arrs a b gt a 0 b 0 上面的数组已排序为 1 100 1 11 2 12 11 22 我希望它们按以下方式排序a 0 b 0 首先 如果
  • phpunit测试调用其他需要mock的类方法的方法

    我正在尝试创建一个非常标准的单元测试 在其中调用一个方法并断言它的响应 但是我正在测试的方法调用同一类中的另一个方法 该方法做了一些繁重的工作 我想模拟该方法 但仍按原样执行我正在测试的方法 仅使用从调用另一种方法返回的模拟值 我简化了示例
  • 总和不小于 key 的数组的最小子集

    给定一个数组 假设为非负整数 我们需要找到最小长度子集 使得元素之和不小于 K K 是作为输入提供的另一个整数 是否有可能找到时间复杂度为 O n n 的大 oh 的解决方案 我目前的想法是这样的 我们可以在 O n log n 中对数组进
  • WordPress 插件中的类自动加载器

    我想编写一个类自动加载器以在 WordPress 插件中使用 该插件将安装在多个站点上 我想尽量减少与其他插件发生冲突的机会 自动加载器将是这样的 function autoload name some code here 我的主要问题是
  • PHP 数组通过 JSON 转为 jquery 数组

    我有点困惑为什么以下不起作用 get php
  • 保存多对多关系,同步/附加不存在?

    我有以下两个多对多关系的模型 use Illuminate Database Eloquent Model class Permission extends Model The database table used by the mode
  • 如何从父类中获取子类名

    我试图在不需要子类上的函数的情况下完成此任务 这可能吗 我有一种感觉 但我真的很想确定

随机推荐