第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問題,去搜搜看,總會(huì)有你想問的

php中的組json嵌套對象

php中的組json嵌套對象

PHP
浮云間 2023-07-01 15:08:25
我有一個(gè) json 響應(yīng),如下所示:$response ='[    {        "userSummaries": [            {                "id": "9910",                "status": "Active",                "name": "Jhon"            }        ]    },    {        "userSummaries": [            {                "id": "8754",                "status": "Active",                "name": "Jane"            }        ]    }]';我想用userSummaries這個(gè) php 代碼將其分組:$myArr = json_decode($response, true);      $result_arr = [];        array_walk($myArr,function($v,$k) use (&$result_arr){       $result_arr[key($v)] = $v[key($v)];     });       echo json_encode($result_arr);并且響應(yīng)只返回一個(gè)數(shù)據(jù):{"userSummaries":[{"id":"8754","status":"Active","name":"Jane"}]}是否可以得到這樣的輸出響應(yīng)?:{"userSummaries":[{"id":"9910","status":"Active","name":"Jhon"}, {"id":"8754","status":"Active","name":"Jane"}, ]}通過網(wǎng)絡(luò)嘗試過,但沒有找到解決方案這是我的腳本: https: //3v4l.org/tVkK5也嘗試過這個(gè):$class_array = array();foreach ($myArr as $sa) {    $class_array[$sa['userSummaries']][] = array('name' => $sa['name']);}但返回:Notice: Undefined index: name in /in/hvSFC on line 28Warning: Illegal offset type in /in/hvSFC on line 28Notice: Undefined index: name in /in/hvSFC on line 28Warning: Illegal offset type in /in/hvSFC on line 28[]需要幫忙
查看完整描述

1 回答

?
哈士奇WWW

TA貢獻(xiàn)1799條經(jīng)驗(yàn) 獲得超6個(gè)贊

你很接近。您只需要在每個(gè)循環(huán)中引用鍵和第一個(gè)userSummaries,而不是使用整個(gè)...


$myArr = json_decode($response, true);

$result_arr = ["userSummaries"=>[]];

foreach($myArr as $user) {

    $result_arr["userSummaries"][] = $user['userSummaries'][0];

}

echo json_encode($result_arr);

結(jié)果是:


{"userSummaries":[

    {"id":"9910","status":"Active","name":"Jhon"},

    {"id":"8754","status":"Active","name":"Jane"}

]}

如果您預(yù)見每個(gè)中的 userSummaries 本身將有多個(gè)用戶......那么這將起作用:


$response ='[

    {

        "userSummaries": [

            {

                "id": "9910",

                "status": "Active",

                "name": "Jhon"

            }

        ]

    },

    {

        "userSummaries": [

            {

                "id": "8754",

                "status": "Active",

                "name": "Jane"

            },

            {

                "id": "5421",

                "status": "Active",

                "name": "Bob"

            }

        ]

    }

]';


$myArr = json_decode($response, true);

$result_arr = ["userSummaries"=>[]];

foreach($myArr as $usergroup) {

    foreach($usergroup['userSummaries'] as $user) {

        $result_arr["userSummaries"][] = $user;

    }

}

echo json_encode($result_arr);

結(jié)果是:


{"userSummaries":[

    {"id":"9910","status":"Active","name":"Jhon"},

    {"id":"8754","status":"Active","name":"Jane"},

    {"id":"5421","status":"Active","name":"Bob"}

]}


查看完整回答
反對 回復(fù) 2023-07-01
  • 1 回答
  • 0 關(guān)注
  • 162 瀏覽

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號