猿问

从其他2个数组构造一个关联数组,并通过一个公共键链接

问题:


我想将2个关联数组组合成一个。要链接这些阵列,两个中都存在ID密钥。


输入:


要使用api调用来检索我的联系人,我必须执行2个请求:首先使用ID检索联系人,并通过电子邮件发送地址,然后获取一些信息,例如姓名,城市等。


第一个返回这样的数组:


$contactArray = array(

    array(

        "CreatedAt" => "2019-04-12T11:53:26Z",

        "DeliveredCount" => 0,

        "Email" => "terry@example.org",

        "ExclusionFromCampaignsUpdatedAt" => "2019-04-28T09:21:35Z",

        "ID" => 1864410583,

        "IsExcludedFromCampaigns" => false,

        "IsOptInPending" => false,

        "IsSpamComplaining" => false,

        "LastActivityAt" => "2019-04-28T09:21:35Z",

        "LastUpdateAt" => "2019-04-28T09:21:35Z",

        "Name" => "",

        "UnsubscribedAt" => "",

        "UnsubscribedBy" => ""

    ),

    array(

        "CreatedAt" => "2019-04-12T12:39:30Z",

        "DeliveredCount" => 0,

        "Email" => "duane@example.org",

        "ExclusionFromCampaignsUpdatedAt" => "",

        "ID" => 1864410588,

        "IsExcludedFromCampaigns" => false,

        "IsOptInPending" => false,

        "IsSpamComplaining" => false,

        "LastActivityAt" => "2019-04-12T12:39:30Z",

        "LastUpdateAt" => "2019-04-12T12:39:30Z",

        "Name" => "",

        "UnsubscribedAt" => "",

        "UnsubscribedBy" => ""

    )

);

第二次调用,返回一个类似的数组


$contactDataArray =

        array(

            array(

                "ContactID" => 1864410583,

                "Data" => array(

                    array(

                        "Name" => "firstname",

                        "Value" => "Mark"

                    ),

                    array(

                        "Name" => "city",

                        "Value" => "Miami"

                    ),

                    array(

                        "Name" => "name",

                        "Value" => "Terry"

                    ),

                    array(

                        "Name" => "phone",

                        "Value" => "555-5555"

                    )

                ),

                "ID" => 1864410583

            ),

在中$contactArray,ID键与ContactID键和ID中的ID键匹配$contactDataArray


沧海一幻觉
浏览 159回答 3
3回答

慕村9548890

对于PHP> = 7.1,可以使用list()使用数组解构<?php&nbsp;$output = [];foreach ($contactDataArray as [&nbsp; &nbsp; 'ID' => $id,&nbsp; &nbsp; 'Data' => [&nbsp; &nbsp; &nbsp; &nbsp; ['Name' => $firstnameKey, 'Value' => $firstnameValue],&nbsp; &nbsp; &nbsp; &nbsp; ['Name' => $cityKey, 'Value' => $cityValue],&nbsp; &nbsp; &nbsp; &nbsp; ['Name' => $nameKey, 'Value' => $nameValue],&nbsp; &nbsp; &nbsp; &nbsp; ['Name' => $phoneKey, 'Value' => $phoneValue]&nbsp; &nbsp; ]]) {&nbsp; &nbsp; $output[] = [&nbsp; &nbsp; &nbsp; &nbsp; "Email" => $contactArray[array_search($id, array_column($contactArray, 'ID'))]['Email'],&nbsp; &nbsp; &nbsp; &nbsp; "ID" => $id,&nbsp; &nbsp; &nbsp; &nbsp; $firstnameKey => $firstnameValue,&nbsp; &nbsp; &nbsp; &nbsp; $cityKey => $cityValue,&nbsp; &nbsp; &nbsp; &nbsp; $nameKey => $nameValue,&nbsp; &nbsp; &nbsp; &nbsp; $phoneKey => $phoneValue&nbsp; &nbsp; ];}var_dump($output);演示版

米脂

您可以使用foreach做到这一点,$result = [];foreach ($contactDataArray as $key => $value) {&nbsp; &nbsp; $ids&nbsp; &nbsp;= array_column($contactArray, "ID"); // fetching all values from contactArray&nbsp; &nbsp; if (!empty(array_intersect([$value['ContactID'], $value['ID']], $ids))) { // checking if both satisfy the condition&nbsp; &nbsp; &nbsp; &nbsp; $result[$key] = array_column($value['Data'], 'Value', 'Name'); // combining name and value&nbsp; &nbsp; &nbsp; &nbsp; // searchng for key with matched ContactID&nbsp; &nbsp; &nbsp; &nbsp; $result[$key]['Email'] = $contactArray[array_search($value["ContactID"], $ids)]['Email'];&nbsp; &nbsp; &nbsp; &nbsp; $result[$key]['ID'] = $value["ContactID"];&nbsp; &nbsp; }}演示。

回首忆惘然

你可以试一下吗?$output = [];for($i = 0; $i < count($contactDataArray); $i++) {&nbsp; &nbsp; $arrIDandEmail = [&nbsp; &nbsp; &nbsp; &nbsp; 'Email' => isset($contactArray[$i]['Email']) ? $contactArray[$i]['Email'] : '',&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; 'ID' => isset($contactDataArray[$i]['ID']) ? $contactDataArray[$i]['ID'] : ''&nbsp; &nbsp; ];&nbsp; &nbsp; $arrData = array_column($contactDataArray[$i]["Data"], "Value", "Name");&nbsp; &nbsp; $newArray = array_merge($arrIDandEmail, $arrData);&nbsp; &nbsp; $output[] = $newArray;}
随时随地看视频慕课网APP
我要回答