如何处理具有多个数据集的laravel获取请求

时间:2019-06-04 06:49:15

标签: php laravel-5

我收到了带有多个数据集的laravel请求表单数据,例如:-

array:5 [▼
  "start_date" => array("2019-01-01","2019-01-02","2019-01-03");
  "planned" => array("1","2","3");
  "planned_inc" => array("2","8","16.5");
  "actual_inc" => array:10("7.9","1.2","3.6");
]

我想操纵像这样的数据集:-

$data = ['2019-01-01', '1', '2', '7.9'];

$formData =  request(['start_date', 'planned', 'planned_incr', 'actual_incr' ,'actual']);
dd($formData);
foreach ($formData as $data) {
    $jso = $data['start_date'];
    $da = $data['planned'];
    print_r($da);
}

1 个答案:

答案 0 :(得分:1)

我认为您想将$formData转换为多维数组,如下所示:

$dataSet = [];

foreach ($formData['start_date'] as $key=> $value) {

    $dataSet[] = array(
        'start_date' => $value,
        'planned'   => (isset($formData['planned'][$key]) ? $formData['planned'][$key]: ''),
        'planned_inc'   => (isset($formData['planned_inc'][$key]) ? $formData['planned_inc'][$key]: ''),
        'actual_inc'   => (isset($formData['actual_inc'][$key]) ? $formData['actual_inc'][$key]: ''),

    );
}


print_r($dataSet);

输出:-{https://3v4l.org/fo4j5