我的PHP函數返回使用與對象陣列的Json encocde混合轉換所有陣列僅
$req = json_encode($questionData);
然後傳遞這對其它功能和解碼 $res = json_decode($req);
陣列,我轉換成JSON它以數組格式返回包含所有索引的數組。 即使在請求數組$ questionData在某個索引上有對象,那些應該在jason編碼和解碼執行後的對象中。
我想獲得具有arrya和對象的數據混合的相同數組。
下面是要求數組
Array
(
[success] => 1
[pagename] => RC1_PGR_Questions
[result] => Array
(
[CurrentInsuranceStatus] => Array
(
[QuestionData] => Platform_Form_Page_Question Object
(
[_answer] =>
[_answerOptions] => Array
(
[0] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340280
[_hideAnswer] => 1
[_order] => 110
[_promptText] => Yes
[_value] => Y
[_errorLog] => Array
(
)
)
[1] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340290
[_hideAnswer] =>
[_order] => 120
[_promptText] => Yes, on parent's policy
[_value] => Parents
[_errorLog] => Array
(
)
)
[2] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340300
[_hideAnswer] =>
[_order] => 130
[_promptText] => Yes, insured through employer
[_value] => Company
[_errorLog] => Array
(
)
)
[3] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340310
[_hideAnswer] =>
[_order] => 140
[_promptText] => No, policy lapsed/expired
[_value] => Lapsed
[_errorLog] => Array
(
)
)
[4] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340320
[_hideAnswer] =>
[_order] => 150
[_promptText] => No, never been insured
[_value] => Never
[_errorLog] => Array
(
)
)
[5] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340330
[_hideAnswer] =>
[_order] => 160
[_promptText] => No, I didn't have a vehicle to insure
[_value] => First
[_errorLog] => Array
(
)
)
[6] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340340
[_hideAnswer] =>
[_order] => 170
[_promptText] => No, been deployed/overseas in the military
[_value] => Military
[_errorLog] => Array
(
)
)
[7] => Platform_Form_Page_Question_AnswerOption Object
(
[_answerKey] => 340350
[_hideAnswer] =>
[_order] => 180
[_promptText] => No, other reason
[_value] => Other
[_errorLog] => Array
(
)
)
)
[_answersDivID] => html_CurrentInsuranceStatus
[_phoneAreaField] =>
[_attributeName] => CurrentInsuranceStatus
[_criteriaBasedQuestion] => 1
[_dateField] =>
[_eventHandler] =>
[_fieldType] => select
[_hideQuestion] => 1
[_monthField] =>
[_phoneNumberField] =>
[_order] => 10000
[_pageAttributes] => stdClass Object
(
[additionalLabel] =>
[answerStyle] =>
[colNum] =>
[colWidth] =>
[cssclass] =>
[defaultValue] =>
[isMultiSelect] =>
[maxLength] =>
[min] =>
[questionSkinStyle] =>
[toolTip] =>
)
[_phonePreFixField] =>
[_questionDivID] => prompt_CurrentInsuranceStatus
[_questionKey] => 7169010
[_required] => 1
[_text] => Current insurance status
[_yearField] =>
[_errorLog] => Array
(
)
)
)
)
)
響應陣列
stdClass Object
(
[success] => 1
[pagename] => RC1_PGR_Questions
[result] => stdClass Object
(
[CurrentInsuranceStatus] => stdClass Object
(
[QuestionData] => stdClass Object
(
[_answer] =>
[_answerOptions] => Array
(
[0] => stdClass Object
(
[_answerKey] => 340280
[_hideAnswer] => 1
[_order] => 110
[_promptText] => Yes
[_value] => Y
[_errorLog] => Array
(
)
)
[1] => stdClass Object
(
[_answerKey] => 340290
[_hideAnswer] =>
[_order] => 120
[_promptText] => Yes, on parent's policy
[_value] => Parents
[_errorLog] => Array
(
)
)
[2] => stdClass Object
(
[_answerKey] => 340300
[_hideAnswer] =>
[_order] => 130
[_promptText] => Yes, insured through employer
[_value] => Company
[_errorLog] => Array
(
)
)
[3] => stdClass Object
(
[_answerKey] => 340310
[_hideAnswer] =>
[_order] => 140
[_promptText] => No, policy lapsed/expired
[_value] => Lapsed
[_errorLog] => Array
(
)
)
[4] => stdClass Object
(
[_answerKey] => 340320
[_hideAnswer] =>
[_order] => 150
[_promptText] => No, never been insured
[_value] => Never
[_errorLog] => Array
(
)
)
[5] => stdClass Object
(
[_answerKey] => 340330
[_hideAnswer] =>
[_order] => 160
[_promptText] => No, I didn't have a vehicle to insure
[_value] => First
[_errorLog] => Array
(
)
)
[6] => stdClass Object
(
[_answerKey] => 340340
[_hideAnswer] =>
[_order] => 170
[_promptText] => No, been deployed/overseas in the military
[_value] => Military
[_errorLog] => Array
(
)
)
[7] => stdClass Object
(
[_answerKey] => 340350
[_hideAnswer] =>
[_order] => 180
[_promptText] => No, other reason
[_value] => Other
[_errorLog] => Array
(
)
)
)
[_answersDivID] => html_CurrentInsuranceStatus
[_phoneAreaField] =>
[_attributeName] => CurrentInsuranceStatus
[_criteriaBasedQuestion] => 1
[_dateField] =>
[_eventHandler] =>
[_fieldType] => select
[_hideQuestion] => 1
[_monthField] =>
[_phoneNumberField] =>
[_order] => 10000
[_pageAttributes] => stdClass Object
(
[additionalLabel] =>
[answerStyle] =>
[colNum] =>
[colWidth] =>
[cssclass] =>
[defaultValue] =>
[isMultiSelect] =>
[maxLength] =>
[min] =>
[questionSkinStyle] =>
[toolTip] =>
)
[_phonePreFixField] =>
[_questionDivID] => prompt_CurrentInsuranceStatus
[_questionKey] => 7169010
[_required] => 1
[_text] => Current insurance status
[_yearField] =>
[_errorLog] => Array
(
)
)
)
)
)
當param 2(assoc)沒有設置時,爲什麼使用json_decode(json_encode())''''看不到json_decode在'{...}'聲明的JSON-Object返回assoc數組? - 嘗試'json_decode($ req,false)' – UnskilledFreak
反正默認值是false,或者我得到數組或對象中的所有索引。我想和我的要求一樣。 –
所以你想要的是在jans解析_answerOptions中的鍵作爲Platform_Form_Page_Question_AnswerOption返回例如?我認爲這是不可能的原因JSON只能處理一個(stdclass)對象或一個int索引數組 – UnskilledFreak