修改php中的数组

这是我创建数组的代码


$insured_data['QuotaDtls']['Riskdtls'] = [

    "InsuredName"=> "Testone", 

];

$insured_data['Authenticate'] =   [

    'WACode' => '0000',

]; 

$insured_data['QuotaDtls'] = [

    'ProductType'=> 'Individual'

];


Output:


{

   "Authenticate": {

         "WACode": "0000",

   },

   "QuotaDtls": {

         "ProductType": "Individual",

         "Riskdtls": {

                "InsuredName": "Testone",

         }

   }

}

我想对此数组进行一些修改,尝试了许多不同的方法但无法做到。这是在代码点火器中完成的,请帮忙。


{

   "Authenticate": {

       "WACode": "0000"

    },

   "QuotaDtls": {

       "ProductType": "Individual",

       "Riskdtls": [

                      {

                          "InsuredName": "Testone",

                      }

        ]

   }

}


慕容708150
浏览 85回答 1
1回答

元芳怎么了

您需要Riskdtls进一步创建一个数组,$insured_data['QuotaDtls']['Riskdtls']如下所示$insured_data['Authenticate'] =&nbsp; &nbsp;[&nbsp; &nbsp; 'WACode' => '0000',];&nbsp;$insured_data['QuotaDtls'] = [&nbsp; &nbsp; 'ProductType'=> 'Individual'];$insured_data['QuotaDtls']['Riskdtls'][] = [&nbsp; &nbsp; "InsuredName"=> "Testone",&nbsp;&nbsp; &nbsp; "entry2"=> "testdata2",&nbsp;&nbsp; &nbsp; "entry3"=> "testdata3"&nbsp;];$json = json_encode($insured_data,JSON_PRETTY_PRINT) ;&nbsp;printf("<pre>%s</pre>", $json);通过使用json_encode函数将数组编码为 JSON 格式来实现所需的输出,最终输出如下:{&nbsp; &nbsp; "Authenticate": {&nbsp; &nbsp; &nbsp; &nbsp; "WACode": "0000"&nbsp; &nbsp; },&nbsp; &nbsp; "QuotaDtls": {&nbsp; &nbsp; &nbsp; &nbsp; "ProductType": "Individual",&nbsp; &nbsp; &nbsp; &nbsp; "Riskdtls": [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "InsuredName": "Testone",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "entry2": "testdata2",&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; "entry3": "testdata3"&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; &nbsp; &nbsp; &nbsp; ]&nbsp; &nbsp; }}
打开App,查看更多内容
随时随地看视频慕课网APP