2017-11-24 1 views
1

zu erstellen habe ich eine API, die eine JSON-String in folgendem Format erwartet:ein Formular mit JSON

{ 
    "Title": "Mr", 
    "Forenames": "Steve", 
    "Surname": "Williams", 
    "CountryOfBirth": 1, 
    "EmailAddress": "[email protected]", 
    "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 
} 

Wie Sie einige innere verschachtelte Elemente sehen können gibt es, wo jeder Wert selbst ein Array sein kann, wie zum als AdditionalTelephone

ich neu erstellt diesen JSON-String in PHP mit dem folgenden:

<?php 

$dataArray = array(
    "Title" => "Mr", 
    "Forename" => "Jesse", 
    "Surname" => "Orange", 
    "CountryOfBirth" => 1, 
    "EmailAddress" => "[email protected]", 
    "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(); 
?> 

Dies druckt die gleiche Art von Sache.

Jetzt habe ich ein Formular, das ein Benutzer ausfüllt und die Daten POSTED, also wollte ich diese Werte in das Array, das ich erstellt hatte, verwenden.

Zum Beispiel:

$dataArray = array(
    "Title" => POST['Title'], 
    etc 
) 

Die Frage ist, wenn ein Wert ist eine Liste der inneren Arrays wie der Fall mit AdditionalCitizenship kann ich Namen von Eingängen als Arrays verwenden?

<input type="text" name=AdditionalCitizenship[Array1]['CountryOfResidency']> 
<input type="text" name=AdditionalCitizenship[Array2]['CountryOfResidency']> 

Antwort

1

Ja, außer Sie

<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]"> 

In Ihrem $_POST Array verwenden würde dies wie

"AdditionalCitizenship" => array(
    array(
     "CountryOfResidency" => 0, 
     "TaxIdentificationNumber" => null 
    ), 
    array(
     "CountryOfResidency" => 0, 
     "TaxIdentificationNumber" => null 
    ) 
) 
+0

aussehen wird, ich werde geben, dass ein zu gehen, und ich werde meine Ergebnisse hinzufügen auf die Frage und akzeptiere deine Antwort. Vielen Dank. –

Verwandte Themen