简体   繁体   English

PHP中的多维数组推送

[英]multidimensional array push in 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"]] ?? [];
}

Expected Output预期产出

$userData = [
    [
        "UID" => "5f10482574d83d4b726fe5",
        "name" => "Yug Gill",
        "orgID" => "5f10481d74d83d4b726",
        "imageURL" => "female.png",
        "UPID" => "5f10482574d83d4b6fe007"
    ]
];

i have two aray UID common for both array, now i want to take UPID from $userProductsData and push into $userData , i have tried not working properly, kindly anyone update my code please ?>我有两个数组通用的数组UID ,现在我想从$userProductsData获取 UPID 并推送到$userData ,我试过无法正常工作,请任何人更新我的代码?>

Try this one.试试这个。


$userData = [
    [
        "UID" => "5f10482574d83d4b726fe5",
        "name" => "Yug Gill",
        "orgID" => "5f10481d74d83d4b726",
        "imageURL" => "female.png"
    ]
];

$userProductsData = [
    [
    "UPID" => "5f10482574d83d4b6fe007",
    "UID" => "5f10482574d83d4b726fe5",
    ]
];
$userDetailsResult = [];

foreach ($userProductsData as $key => $value) {
    $userData[$key]["UPID"] = $value['UPID'];
   
}

print_r($userData);

Your $userProductsData is a regular array, it has indexes 0, 1, 2 etc... Then you are trying to get an item from this array by string key "5f10482574d83d4b726fe5" .您的$userProductsData是一个常规数组,它具有索引 0、1、2 等......然后您试图通过字符串键"5f10482574d83d4b726fe5"从该数组中获取一个项目。

$userProductsData should be a key array like that: $userProductsData应该是这样的键数组:

$userProductsData = [
    "5f10482574d83d4b726fe5" => [
        "UPID" => "5f10482574d83d4b6fe007",
        "UID" => "5f10482574d83d4b726fe5",
    ],
];

Then you can get an item from this array by key "5f10482574d83d4b726fe5" .然后你可以通过键"5f10482574d83d4b726fe5"从这个数组中获取一个项目。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM