1

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.

Cœur
39k25 gold badges206 silver badges281 bronze badges
asked Sep 26, 2018 at 11:21
4
  • 1
    You can't have multiple values in a PHP array with the same key (FieldName and Values). Commented Sep 26, 2018 at 11:27
  • @Nick, Yes that is exact first issue I have notices but after wrapping each element with array(); like "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" ] } Commented Sep 26, 2018 at 11:30
  • 1
    You should not be getting numeric keys in your JSON, unless you specify it as an option. What is your exact json_encode call? Commented Sep 26, 2018 at 11:34
  • @jh1711 its $putdata = stripslashes(json_encode($postArray)); Commented Sep 26, 2018 at 11:39

2 Answers 2

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

answered Sep 26, 2018 at 11:32

2 Comments

To reply to your (now deleted) comment to my (now deleted) comment. Yes, you are correct, I misread their code.
Thanks for suggestions
2

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"
 }
 }
 ]
}
answered Sep 26, 2018 at 11:41

2 Comments

@WasimSayyed this is basically identical to my answer (stackoverflow.com/a/52516751/5947043) posted 10 minutes earlier, but without the benefit of a live demo or any actual explanation of the problem, yet you accept and upvote this, and ignore mine? There's nothing wrong with this answer per se (although I wouldn't upvote a code-only answer personally), but it's just a duplicate of an earlier one.
@WasimSayyed I'm not sure what you mean? I posted a live demo which proves that it solves the issue. And if I didn't think it solved the issue, I wouldn't write an Answer - what would be the point of that?

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.