Reputation: 2377
My php function returns an array that I converted into json using
$req = json_encode($questionData);
then passed this to other function and decoded
$res = json_decode($req);
It return me an array including all indexes in array format. Even in request array $questionData has object on some index, those should be in object after jason encode and decode execution.
I want to get same array which has mix of data of arrya and object.
Below is request array
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
(
)
)
)
)
)
Response 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
(
)
)
)
)
)
Upvotes: 1
Views: 57
Reputation: 306
so after i realised what you realy want to do i just uses serialize
and unserialize
as alternatives to the JSON-functions like the following working example:
class testobj {
public function __get($name) {
if(isset($this->{$name}))
return $this->{$name};
return false;
}
public function __set($name,$val) {
$this->{$name} = $val;
}
}
$test = new testobj();
$test->testvar = "testval";
echo print_r($test,true);
/* Output:
testobj Object
(
[testvar] => testval
)
*/
$test = serialize($test);
echo $test;
/* Output:
O:7:"testobj":1:{s:7:"testvar";s:7:"testval";}testobj Object
*/
$test = unserialize($test);
echo print_r($test,true);
/* Output:
testobj Object
(
[testvar] => testval
)
*/
echo $test->testvar; // outputs "testval"
i think serialize()
and and unserialize()
are not that fast as JSON de-/encoding (i've never tested/benchmarked this) but it stores object names in itself. I dont know if it works for classes/objects with methods to, but for your data above it will work. Also with the multidimensional structure, too.
Upvotes: 0