2017-06-22 65 views
1

我试图插入数据到json中的数据库,其中多个select有多个选项标签和值。只要我想要插入到数据库这样的: -如何在foreach数组中使用foreach?

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; 
} 
+3

你不能用'的foreach 'array'里面' –

+0

好我的问题是由于foreach内数组。那么,我该如何在数组中实现foreach。无论如何去做。 –

+0

使用'foreach'生成第二个数组,然后将其放入第一个数组,然后将其放入您想要的数组中。 – CD001

回答

3

试试这个代码:

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; 
} 
+0

感谢您的回答@rya。我会尝试 –

+1

哦,最后它是工作。 –

+0

在这里,'$ key'被覆盖!! –

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; 
} 
+0

谢谢@Shaunak它在这一行显示未定义的索引$ request ['drop-option-label'] [$ key] [$ index] –

+0

我们需要在转换器中添加'!empty'来检查值。 –

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; 
} 
+0

谢谢你的答案,但它仍然有问题。 –

+0

@PawanDongol ...你能更具体一点吗? – CD001

+0

ok我的问题显示错误undefined index offset 0在这行'label'=>($ request ['drop-option-label'] [$ key] [$ index])? ($ request ['drop-option-label'] [$ key] [$ index]):null –