douzuo0002 2018-10-10 13:16
浏览 173
已采纳

从第二个数组中获取数据并将值放入数组1中

I have the following situation and cannot find an easy way to do it.

I have 2 arrays:

1)

Array (
[0] => Array
    (
        [FirstName] => Tom
        [LastName] => Siemens
        [Id] => 10300
    )

[1] => Array
    (
        [FirstName] => Sam
        [LastName] => Tailor
        [Id] => 10301
    )

2)

Array
    (
    [0] => Array
           (
            [Type] => Invoice
            [Number] => 6344394
            [Project] => Array
                (
                    [Name] => Test Project 1
                    [ResponsibleUserId] => 10300
                    [Id] => 498
                    [ResponsibleUser] => 
                )

        )
    [1] => Array (
            [Type] => Invoice
            [Number] => 6345555
            [Project] => Array
                (
                    [Name] => Test Project 2
                    [ResponsibleUserId] => 10301
                    [Id] => 499
                    [ResponsibleUser] => 
                )

        )

What could be the best approach to get the "FirstName LastName" from the first array depends on the ID which must equal the Project Id from the second array and to put these values inside the second array -> Project -> ResponsibleUser?

The result I'm looking for is the following:

Final Array)

Array (
    [0] => Array
           (
            [Type] => Invoice
            [Number] => 6344394
            [Project] => Array
                (
                    [Name] => Test Project 1
                    [ResponsibleUserId] => 10300
                    [Id] => 498
                    [ResponsibleUser] => Tom Siemens
                )

        )
    [1] => Array (
            [Type] => Invoice
            [Number] => 6345555
            [Project] => Array
                (
                    [Name] => Test Project 2
                    [ResponsibleUserId] => 10301
                    [Id] => 499
                    [ResponsibleUser] => Sam Tailor
                )

        )
  • 写回答

3条回答 默认 最新

  • dsa45132 2018-10-10 13:24
    关注

    If you start by indexing the first array by the id using array_column() ...

    $idList = array_column($array1, null, "Id");
    

    You can then just loop over the second array and pick out the name each time and update it...

    foreach (array2 as &$project ) {
        $id = $project["Project"]["ResponsibleUserId"];
        $project["Project"]["ResponsibleUser"] = $idList[$id]["FirstName"]." ".$idList[$id]["LastName"];
    }
    

    Use &$project as this allow you to update the original value which is what your after.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分
  • ¥15 delphi webbrowser组件网页下拉菜单自动选择问题
  • ¥15 linux驱动,linux应用,多线程