2016-01-06 19 views
-2

我需要生成以下JSON:建設一個PHP數組,將作出具體的JSON

{ 
"title": "my form", 
"fields": [ 
{ 
    "type": "short_text", 
    "question": "What is your name?" 
}, 
{ 
    "type": "multiple_choice", 
    "question": "How often do you want to receive emails?", 
    "choices": [ 
    { 
     "label": "Daily" 
    }, 
    { 
     "label": "Weekly" 
    }, 
    { 
     "label": "Monthly" 
    },] 
},] 

我想這個PHP代碼做到這一點:

$data = array(
    "title" => "my form", 
    "fields" => array ( 
        array (
         "type" => "short_text", 
         "question" => "What is your name?" 
       ), 
        array ( 
         array (
           "type" => "multiple_choice", 
           "question" => "How often do you want to receive emails?", 
           "choices" => (
            array ("label" => "Daily") 
          ), 
           (array ("label" => "Weekly")), 
           (array ("label" => "Monthly")) 
         ) 
        ) 
       ) 
); 
$output = json_encode($data); 

...但它不工作。

我會很感激任何幫助,你們可以提供!

+0

若你要格式化您的代碼正確,你會看到你犯過的錯誤:在域陣列你堆了太多陣列BF「多項選擇」,然後在「選擇」你有一個語法錯誤('('expect expect'array'(') – Jeff

+0

我對代碼進行了格式化,就像其他人閱讀這些內容時的信息一樣,我想知道爲什麼我抱怨沒有格式化的代碼......) – Jeff

回答

1
<?php 
$data = array (
    'title' => 'my form', 
    'fields' => 
    array (
    array (
     'type' => 'short_text', 
     'question' => 'What is your name?', 
    ), 
    array (
     'type' => 'multiple_choice', 
     'question' => 'How often do you want to receive emails?', 
     'choices' => 
     array (
     array (
      'label' => 'Daily', 
     ), 
     array (
      'label' => 'Weekly', 
     ), 
     array (
      'label' => 'Monthly', 
     ), 
    ), 
    ), 
), 
); 
$output = json_encode($data); 
?> 
+0

謝謝,這個工作完美 - 非常有用尋求幫助! – crule