希望这回答你的问题做出。我正在做类似的事情。
// Sample JSON where student has diploma.
{
"High_School": "American High School",
"HS_Diploma": true,
"HS_Diploma_date": "30-JUN-1997"
}
// Sample JSON where student does not have diploma.
{
"High_School": "American High School",
"HS_Diploma": false,
"HS_Diploma_date": null
}
$diploma = false; // Set default to false.
$diploma_date = null; // Set default data to null.
// Get Radio button 'diploma' which is set to 1 for yes.
if ($form->get("diploma")->getData() == 1){
$diploma = true; // Set boolean to true for yes.
}
if($diploma){ // If above boolean is true, student has HS diploma
$diploma_date = $form->get("diploma_date")->getData(); // So get the date.
}
// Creates a PHP array.
$high_school_array = array(
"High_School" => $form->get("high_school")->getData(),
"HS_Diploma" => $diploma,
"HS_Diploma_date" => $diploma_date->format('d-M-Y')
);
// Encode to JSON and set in Entity (here called $app), where setHSInfo() is the $app Entity
// function that takes in a string (JSON encoded).
$app->setHSInfo(json_encode($high_school_array));
// Store to database.
$em->persist($app);
$em->flush();
然后在一个控制器,从数据库得到的,你需要解码的JSON,然后传递给 您的嫩枝文件,你可以轻松使用它:
// Where getAppHSInfo function returns the $high_school_array encoded array above from the db.
return $this->render('viewHSDetails.html.twig', array(
'hs_json' => json_decode($app->getAppHSInfo(), true),
));
然后在枝条文件,你可以方便地使用它:
Education Information:<br/>
High School attended: {{ hs_json['High_School'] }}<br/>
{% if hs_json['HS_Diploma'] %}
Diploma Yes - Date: {{ hs_json['HS_Diploma_date'] }}
{% else %}
Diploma No
{% endif %}
您可以用'json_encode()'用JSON编码数组(我假设,因为PHP其你使用的标签)。 http://php.net/manual/en/function.json-encode.php – Kisaragi
这就是你如何使用树枝制作数组,我也需要帮助。 – UnknownPerson
嗯,我看我想。你想要一个数组?你在循环数据吗?你想解析文本吗?这太模糊了。 – Kisaragi