PHP 中的多维数组推送

  <?php

$userData = [

    [

        "UID" => "5f10482574d83d4b726fe5",

        "name" => "Yug Gill",

        "orgID" => "5f10481d74d83d4b726",

        "imageURL" => "female.png"

    ]

];


$userProductsData = [

    [

    "UPID" => "5f10482574d83d4b6fe007",

    "UID" => "5f10482574d83d4b726fe5",

    ]

];

$userDetailsResult = [];

foreach ($userData as $key => $value) {

    $userData[$key]["UPID"] = $userProductsData[$value["UID"]] ?? [];

}

预期输出


$userData = [

    [

        "UID" => "5f10482574d83d4b726fe5",

        "name" => "Yug Gill",

        "orgID" => "5f10481d74d83d4b726",

        "imageURL" => "female.png",

        "UPID" => "5f10482574d83d4b6fe007"

    ]

];

我有两个UID数组通用的数组,现在我想从中获取 UPID$userProductsData并推入$userData,我尝试过无法正常工作,请有人更新我的代码吗?>


摇曳的蔷薇
浏览 128回答 2
2回答

猛跑小猪

试试这个。$userData = [&nbsp; &nbsp; [&nbsp; &nbsp; &nbsp; &nbsp; "UID" => "5f10482574d83d4b726fe5",&nbsp; &nbsp; &nbsp; &nbsp; "name" => "Yug Gill",&nbsp; &nbsp; &nbsp; &nbsp; "orgID" => "5f10481d74d83d4b726",&nbsp; &nbsp; &nbsp; &nbsp; "imageURL" => "female.png"&nbsp; &nbsp; ]];$userProductsData = [&nbsp; &nbsp; [&nbsp; &nbsp; "UPID" => "5f10482574d83d4b6fe007",&nbsp; &nbsp; "UID" => "5f10482574d83d4b726fe5",&nbsp; &nbsp; ]];$userDetailsResult = [];foreach ($userProductsData as $key => $value) {&nbsp; &nbsp; $userData[$key]["UPID"] = $value['UPID'];&nbsp; &nbsp;}print_r($userData);

慕工程0101907

您$userProductsData是一个常规数组,它有索引 0、1、2 等...然后您尝试通过字符串 key 从此数组中获取项目"5f10482574d83d4b726fe5"。$userProductsData应该是这样的键数组:$userProductsData = [&nbsp; &nbsp; "5f10482574d83d4b726fe5" => [&nbsp; &nbsp; &nbsp; &nbsp; "UPID" => "5f10482574d83d4b6fe007",&nbsp; &nbsp; &nbsp; &nbsp; "UID" => "5f10482574d83d4b726fe5",&nbsp; &nbsp; ],];然后你可以通过 key 从这个数组中获取一个项目"5f10482574d83d4b726fe5"。
打开App,查看更多内容
随时随地看视频慕课网APP