我想在laravel 5.2中使用laravel 5.2将数组数组拆分为相应的数组

时间:2016-05-11 07:10:55

标签: php arrays sorting parsing laravel-5.2

我有一个包含多种形式数据的数组 我想将每个表单的数据拆分成一个数组,这样我就可以将它存储到数据库中 目前在做什么

public function storeWBS(Request $request)
    {
        $value = $request->all();
        $formValue = new WorkBreakdownStructure;

        $count = 0;
     $data_array1 = array();
         foreach ($value as $key => $val2) {
            $data_array= $val2;
                    if (is_array($val2)) {
                    array_push($data_array1, $val2);

            }

        }
        dd($value);
        exit;
        $data_array1->save();
}

并获得此结果

array:4 [▼
  "_token" => "bcK0e9z168ib7rbSZpoRPLWbhx3bRIHq1NqzfNeX"
  "idea_id" => array:3 [▼
    0 => "1"
    1 => "1"
    2 => "1"
  ]
  "wbs_description" => array:3 [▼
    0 => "Visit Campus to read about making great videos and more"
    1 => "Visit Campus to read about \r\n"
    2 => "Visit Campus to read about making great"
  ]
  "percentage" => array:3 [▼
    0 => "30"
    1 => "30"
    2 => "40"
  ]
] 

我想让内部数组的索引0的所有值都在一个数组中 即,

"idea_id" 
      =>  "1"
"wbs_description" 
      => "Visit Campus to read about making great videos and more"
  "percentage" 
      =>  "30"

从上面我试图运行内部循环,但它将导致相同的3个数组

1 个答案:

答案 0 :(得分:0)

foreach ($arr as $key => $val2) {
$data_array= $val2;
    if (is_array($val2)) {
        $count = 0;
        foreach ($val2 as $k=>$v) {
            $data_array1[$count][$key] = $v;
            $count++;
        }
    }
}