2016-04-06 3 views
1

Meine PHP-Funktion ein Array zurückgibt, die ich in jsonJson encocde Mix von Array mit Objekten konvertiert alle in Array nur

$req = json_encode($questionData);

dann diese an andere Funktion umgewandelt geben und decodiert $res = json_decode($req);

Es liefert mir ein Array mit allen Indizes im Array-Format. Selbst im Anfrage-Array $ questionData hat Objekt auf einem bestimmten Index, diese sollten nach jason encode und decode Ausführung im Objekt sein.

Ich möchte das gleiche Array, die Mischung von Daten von Arrya und Objekt hat.

Im Folgenden finden Sie Anfrage 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 
           (
           ) 

         ) 

       ) 



     ) 

) 
+0

hat nie gesehen, dass json_decode assoc-Array auf JSON-Object zurückgibt, das von '{...}' deklariert wird, wenn param 2 (assoc) nicht gesetzt ist - warum wird 'json_decode (json_encode())' '? - probiere 'json_decode ($ req, false)' – UnskilledFreak

+0

Anyways default ist false, Entweder bekomme ich den ganzen Index in Array oder Object. Ich möchte das gleiche wie ich in Anfrage hatte. –

+0

Was willst du also, nachdem die JSON-Analyse der Schlüssel in _answerOptions zum Beispiel als Platform_Form_Page_Question_AnswerOption zurückgegeben wird? Ich denke, das ist nicht möglich Ursache Json kann nur ein (Stdclass) -Objekt oder ein int-indizierten Array handlich – UnskilledFreak

Antwort

0

so, nachdem ich was realisiert man wirklich tun möchte ich folgenden nur verwendet serialize und unserialize als Alternativen zu den JSON-Funktionen wie die Arbeitsbeispiel:

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" 

ich denke und und unserialize() sind nicht so schnell wie JSON de/encoding (ich habe das nie getestet/getestet), aber es speichert Objektnamen in sich. Ich weiß nicht, ob es für Klassen/Objekte mit Methoden funktioniert, aber für Ihre Daten oben wird es funktionieren. Auch mit der multidimensionalen Struktur.

+0

mein Haupt-Array haben Objekte, wird es Array von Objekten geben. –

+0

können Sie sie mischen, wie Sie wollen, Objekt in Array in Objekt in Array oder etwas anderes, auch wenn es wie Objekt, Objekt, Array, Objekt wird es funktionieren – UnskilledFreak

Verwandte Themen