2016-04-06 41 views
1

我的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 
           (
           ) 

         ) 

       ) 



     ) 

) 
+0

當param 2(assoc)沒有設置時,爲什麼使用json_decode(json_encode())''''看不到json_decode在'{...}'聲明的JSON-Object返回assoc數組? - 嘗試'json_decode($ req,false)' – UnskilledFreak

+0

反正默認值是false,或者我得到數組或對象中的所有索引。我想和我的要求一樣。 –

+0

所以你想要的是在jans解析_answerOptions中的鍵作爲Platform_Form_Page_Question_AnswerOption返回例如?我認爲這是不可能的原因JSON只能處理一個(stdclass)對象或一個int索引數組 – UnskilledFreak

回答

0

所以之後我意識到你真的想要做的,我只是使用serializeunserialize作爲替代品的JSON-功能,如以下工作示例:

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" 

我想serialize()unserialize()並不像JSON de-/encoding(我從來沒有測試/基準化過)那麼快,但它本身存儲了對象名稱。我不知道它是否適用於具有方法的類/對象,但對於您的數據,它將起作用。也是多維結構。

+0

我的主數組有對象,它會給出對象的數組。 –

+0

你可以像你想要的那樣混合它們,在數組中的對象或其他東西中的對象,即使它的對象,對象,數組,對象也可以工作 – UnskilledFreak