我正在尝试将数据插入到json中的数据库中,其中多个select具有多个选项标签和值。我只想插入这样的数据库: -
First select dropdown:-
{"select":[{"placeholder":"test","options":
[{"label":"option-label-1","value":"option-value-1"},
{"label":"option-label-2","value":"option-value-2"},
{"label":"option-label-3","value":"option-value-3"},
{"label":"option-label-4","value":"option-value-4"}],
"type":"select","name":"test","label":"test"},
Second select dropdown:-
{"placeholder":"test2","options":
[{"label":"option-label-1","value":"option-value-1"},
{"label":"option-label-2","value":"option-value-2"},
{"label":"option-label-3","value":"option-value-3"},
{"label":"option-label-4","value":"option-value-4"}],
"type":"select","name":"test2","label":"test2"}]}
但是我的代码在插入时显示错误
Parse error: syntax error, unexpected 'foreach' (T_FOREACH), expecting ']'
获取结果的代码是
publicfunction dropDownField($request)
{
$value = [];
if (key_exists("drop-name", $request)) {
foreach ($request['drop-name'] as $key => $data) {
$value[] = [
"type" => ($request['drop-type'][$key]) ? (($request['drop-type'][$key])) : null,
"label" => ($request['drop-label'][$key]) ? ($request['drop-label'][$key]) : null,
"name" => ($request['drop-name'][$key]) ? ($request['drop-name'][$key]) : null,
"placeholder" => ($request['drop-placeholder'][$key]) ? ($request['drop-placeholder'][$key]) : null,
foreach ($request['drop-option-label'] as $index => $data) {
"options" => [
'label' => ($request['drop-option-label'][$key][$index]) ? ($request['drop-option-label'][$key][$index]) : null,
'value' => ($request['drop-option-value'][$key][$index]) ? ($request['drop-option-value'][$key][$index]) : null,
]
}
];
}
}
return $value;
}
答案 0 :(得分:4)
您需要在循环中创建另一个数组,然后根据需要将其添加到原始数组中:
publicfunction dropDownField($request)
{
$value = [];
if (key_exists("drop-name", $request)) {
foreach ($request['drop-name'] as $key => $data) {
// new "options" array
$options = [];
foreach($request['drop-option-label'] as $index => $data) {
$options[] = [
'label' => $data ?: null,
'value' => !empty($request['drop-option-value'][$index]) ? $request['drop-option-value'][$index] : null,
];
}
// original "return" array
$value[] = [
"type" => !empty($request['drop-type'][$key]) ? $request['drop-type'][$key] : null,
"label" => !empty($request['drop-label'][$key]) ? $request['drop-label'][$key] : null,
"name" => !empty($request['drop-name'][$key]) ? $request['drop-name'][$key] : null,
"placeholder" => !empty($request['drop-placeholder'][$key]) ? $request['drop-placeholder'][$key] : null,
"options" => $options // <-- new "options" array added here
];
}
}
return $value;
}
答案 1 :(得分:4)
试试这段代码:
public function dropDownField($request)
{
$value = [];
if (key_exists("drop-name", $request)) {
foreach ($request['drop-name'] as $key => $data) {
$options = [];
foreach($request['drop-option-label'] as $key => $data) {
$options[] = [
'label' => ($request['drop-option-label'][$key]) ?($request['drop-option-label'][$key]) : null,
'value' => ($request['drop-option-value'][$key]) ? ($request['drop-option-value'][$key]) : null,
];
}
$value[] = [
"type" => ($request['drop-type'][$key]) ? (($request['drop-type'][$key])) : null,
"label" => ($request['drop-label'][$key]) ? ($request['drop-label'][$key]) : null,
"name" => ($request['drop-name'][$key]) ? ($request['drop-name'][$key]) : null,
"placeholder" => ($request['drop-placeholder'][$key]) ? ($request['drop-placeholder'][$key]) : null,
"options" => $options
];
}
}
return $value;
}
答案 2 :(得分:2)
它可以是这样的:
public function dropDownField($request)
{
$value = [];
if (key_exists("drop-name", $request)) {
foreach ($request['drop-name'] as $key => $data) {
/* Defined $var here for option */
foreach ($request['drop-option-label'] as $index => $data) {
$var = [
'label' => !empty($request['drop-option-label'][$index]) ? $request['drop-option-label'][$index] : null,
'value' => !empty($request['drop-option-value'][$index]) ? $request['drop-option-value'][$index] : null,
];
}
$value[] = [
"type" => !empty($request['drop-type'][$key]) ? $request['drop-type'][$key] : null,
"label" => !empty($request['drop-label'][$key]) ? $request['drop-label'][$key] : null,
"name" => !empty($request['drop-name'][$key]) ? $request['drop-name'][$key] : null,
"placeholder" => !empty($request['drop-placeholder'][$key]) ? $request['drop-placeholder'][$key] : null,
"options" => $var; //Defined outside foreach loop
];
}
}
return $value;
}