I am creating a array for posting candidate details to an API. that API accepts data in JSON format, I am creating a data for the API using PHP array and using JSON encode to convert it JSON array. but not able to get intended format
PHP Code
$postArray = array(
"DefaultCurrency" => "USD",
"UserName" => "data",
"Photograph" => "data",
"PhotographThumb" => "data",
"Group" => "data",
"Summary" => "data",
"ResumeText" => "data",
"RollupListMembership" => array(
"RollupCode" => "data"
),
"CustomFields" => array(
"FieldName" => "Major",
"Values" => array(
$finalResumeData-> "data"
),
"FieldName" => "Years of Experience",
"Values" => array(
$MonthsOfWorkExperience
),
"FieldName" => "Executive Type",
"Values" => array(
$finalResumeData-> "data"
),
)
);
If i run this code as it is its showing only last result of the CustomFields
I have tried using array()
for individual CustomFields in the array like
"CustomFields" => array(
array("FieldName" => "Major",
"Values" => array(
$finalResumeData-> "data"
)),
I am getting the results as
CustomFields: {
"0": {
"FieldName" : "Value",
"Values": ["data"]
}
}
Intended result
"CustomFields": [
{
"FieldName": "string",
"FieldType": "string",
"Values": [
"string"
]
},
{
"FieldName": "string",
"FieldType": "string",
"Values": [
"string"
]
}
],
So what should I update in the PHP array to get intended results.
2 Answers 2
Inside "CustomFields" you're repeating the same keys again within one array, so they will just overwrite each other. Instead you need an array of separate objects (represented by associative arrays in PHP), i.e. just the same structure as you've shown in your desired JSON output.
"CustomFields" => array(
array(
"FieldName" => "Major",
"Values" => array(
$finalResumeData->data
)
),
array(
"FieldName" => "Years of Experience",
"Values" => array(
$MonthsOfWorkExperience
)
),
array(
"FieldName" => "Executive Type",
"Values" => array(
$finalResumeData->data
)
),
)
N.B. This assumes you're using json_encode($postArray);
without any extra options.
Demo: https://eval.in/1059736
2 Comments
I have just modified your code little bit and getting the output you wanted.
$postArray = array(
"DefaultCurrency" => "USD",
"UserName" => "data",
"Photograph" => "data",
"PhotographThumb" => "data",
"Group" => "data",
"Summary" => "data",
"ResumeText" => "data",
"RollupListMembership" => array(
"RollupCode" => "data"
),
"CustomFields" => array(
array("FieldName" => "Major",
"Values" => array(
'dsadas'=> "data"
)),
array("FieldName" => "Years of Experience",
"Values" => array(
'rewrew'=>'dsa'
)),
array("FieldName" => "Executive Type",
"Values" => array(
'test'=> "data"
)),
)
);
echo "<pre>";print_r($postArray);
echo json_encode($postArray);
?>
Here is the output I am getting.
{
"DefaultCurrency":"USD",
"UserName":"data",
"Photograph":"data",
"PhotographThumb":"data",
"Group":"data",
"Summary":"data",
"ResumeText":"data",
"RollupListMembership":{
"RollupCode":"data"
},
"CustomFields":[
{
"FieldName":"Major",
"Values":{
"dsadas":"data"
}
},
{
"FieldName":"Years of Experience",
"Values":{
"rewrew":"dsa"
}
},
{
"FieldName":"Executive Type",
"Values":{
"test":"data"
}
}
]
}
2 Comments
Explore related questions
See similar questions with these tags.
FieldName
andValues
)."CustomFields" => array( array("FieldName" => "Major", "Values" => array( $finalResumeData-> "data" )),
I am getting the results as 0, 1... keys but I want it to be{ "FieldName": "string", "FieldType": "string", "Values": [ "string" ] }
json_encode
call?$putdata = stripslashes(json_encode($postArray));