我有一个API,它需要一个JSON字符串,格式如下:
{
"Title": "Mr",
"Forenames": "Steve",
"Surname": "Williams",
"CountryOfBirth": 1,
"EmailAddress": "john.doe@email.com",
"EmailType": "Personal",
"BirthDate": "\/Date(632880000000)\/",
"Suffix": null,
"NationalInsuranceNumber": null,
"PrimaryAddress": {
"Address1": "Flat 1",
"Address2": "Oxford Street",
"City": "London",
"County": "London",
"Postcode": "L12456",
"Country": 1
},
"AdditionalAddresses": [
{
"Address1": null,
"Address2": null,
"City": null,
"County": null,
"Postcode": null,
"Country": 0,
"AddressType": 0
}
],
"PrimaryTelephone": {
"Number": "123456789",
"DialingCode": 1,
"TelephoneType": 1
},
"AdditionalTelephone": [
{
"Number": null,
"DialingCode": 0,
"TelephoneType": 0
}
],
"BankAccount": {
"AccountName": "John Doe Account",
"AccountNumber": "123456789",
"SortCode": "123456"
},
"PrimaryCitizenship": {
"CountryOfResidency": 1,
"TaxIdentificationNumber": "AB12CD34EF56"
},
"AdditionalCitizenship": [
{
"CountryOfResidency": 0,
"TaxIdentificationNumber": null
}
],
"ExternalCustomerId": "151",
"ExternalPlanId": "151",
"PlanType": 10
}
正如您所看到的,有一些内部嵌套元素,其中每个值本身可以是一个数组,例如AdditionalTelephone
我用PHP重新创建了这个JSON字符串:
<?php
$dataArray = array(
"Title" => "Mr",
"Forename" => "Jesse",
"Surname" => "Orange",
"CountryOfBirth" => 1,
"EmailAddress" => "email@gmail.com",
"EmailType" => "Personal",
"BirthDate" => "\/Date(632880000000)\/",
"Suffix" => null,
"PrimaryAddress" => array(
"Address1" => "Flat 1",
"Address2" => "Oxford Street",
"City" => "London",
"County" => "London",
"Postcode" => "L12456",
"Country" => 1
),
"AdditionalAddresses" => array(
array(
"Address1" => null,
"Address2" => null,
"City" => null,
"County" => null,
"Postcode" => null,
"Country" => 0,
"AddressType" => 0
)
),
"PrimaryTelephone" => array(
"Number" => "123456789",
"DialingCode" => 1,
"TelephoneType" => 1
),
"AdditionalTelephone" => array(
array(
"Number" => "123456789",
"DialingCode" => 1,
"TelephoneType" => 1
)
),
"BankAccount" => array(
"AccountName" => "John Doe Account",
"AccountNumber" => "123456789",
"SortCode" => "123456"
),
"PrimaryCitizenship" => array(
"CountryOfResidency" => 1,
"TaxIdentificationNumber" => "AB12CD34EF56"
),
"AdditionalCitizenship" => array(
array(
"CountryOfResidency" => 0,
"TaxIdentificationNumber" => null
)
),
"ExternalCustomerId" => "151",
"ExternalPlanId" => "151",
"PlanType" => 10
);
header("Content-type:application/json");
$jsonDataArray = json_encode($dataArray, JSON_PRETTY_PRINT);
echo $jsonDataArray;
die();
?>
这打印出同样的东西。
现在,我有一个用户填写的表单,数据已经过POST,所以我想在我创建的数组中使用这些值。
例如:
$dataArray = array(
"Title" => POST['Title'],
etc
)
问题是:当一个值是内部数组的列表时,例如AdditionalCitizenship
的情况,我可以使用输入名称作为数组吗?
<input type="text" name=AdditionalCitizenship[Array1]['CountryOfResidency']>
<input type="text" name=AdditionalCitizenship[Array2]['CountryOfResidency']>
答案 0 :(得分:1)
是的,除非您使用
<input type="text" name="AdditionalCitizenship[0][CountryOfResidency]">
<input type="text" name="AdditionalCitizenship[0][TaxIdentificationNumber]">
<input type="text" name="AdditionalCitizenship[1][CountryOfResidency]">
<input type="text" name="AdditionalCitizenship[1][TaxIdentificationNumber]">
在$_POST
数组中,这看起来像
"AdditionalCitizenship" => array(
array(
"CountryOfResidency" => 0,
"TaxIdentificationNumber" => null
),
array(
"CountryOfResidency" => 0,
"TaxIdentificationNumber" => null
)
)