从多维关联后数组创建关联数组- Codeigniter



我创建了一个生成多维post数组的表单。我想把它改成数组,这样我就能把它插入到表中。

这是返回的post数组。

Array
(
  [project_no] => 160
  [result] => Array
    (
        [5] => Array
            (
                [temp_dwg_rev] => D
                [temp_dwg_id] => 5
            )
        [6] => Array
            (
                [temp_dwg_rev] => D
                [temp_dwg_id] => 6
            )
        [7] => Array
            (
                [temp_dwg_rev] => E
                [temp_dwg_id] => 7
            )
    )

    [client] => Array
        (
            [1] => Array
                (
                    [client_id] => 1
                )
            [3] => Array
                (
                    [client_id] => 3
                )
        )
)

所以我认为数组应该是这样的,以使查询工作

Array
(
[0] => Array
    (
        [temp_dwg_id] => 5
        [temp_dwg_rev] => D
        [project_no] => 160
        [client_id] => 1
    )
[1] => Array
    (
        [temp_dwg_id] => 6
        [temp_dwg_rev] => D
        [project_no] => 160
        [client_id] => 1
    )
[2] => Array
    (
        [temp_dwg_id] => 7
        [temp_dwg_rev] => E
        [project_no] => 160
        [client_id] => 1
    )
[3] => Array
    (
        [temp_dwg_id] => 5
        [temp_dwg_rev] => D
        [project_no] => 160
        [client_id] => 3
    )
[4] => Array
    (
        [temp_dwg_id] => 6
        [temp_dwg_rev] => D
        [project_no] => 160
        [client_id] => 3
    )
[5] => Array
    (
        [temp_dwg_id] => 7
        [temp_dwg_rev] => E
        [project_no] => 160
        [client_id] => 3
    )
)

数据库中的表是这样的。说明slip_id是自动递增的。

|slip_id |project_no |client_id |temp_dwg_id |temp_dwg_rev
|1       |160        |1         |5           |D
|2       |160        |1         |6           |D
|3       |160        |1         |7           |E
|4       |160        |3         |5           |D
|5       |160        |3         |6           |D
|6       |160        |3         |7           |E

我试过下面的代码,但这创建了一个数组,只有temp_dwg_id, temp_dwg_rev和project_on在一个关联数组。我仍然需要将客户端数组添加到

        $drawings = $_POST['result'];
        $dist = $_POST['client'];
        $project_no = $_POST['project_no'];
        $test = array();
        foreach ($drawings as $row)
        {
            $test[$row['temp_dwg_id']]['temp_dwg_id']= $row['temp_dwg_id'];
            $test[$row['temp_dwg_id']]['temp_dwg_rev']= $row['temp_dwg_rev'];
            $test[$row['temp_dwg_id']]['project_no']= $project_no;
        }

试一试:

foreach($dist as $client)
{
  foreach($drawings as $drawing)
  {
    //determine the index for your current drawings
    $index = count($test);
    $test[$index]['temp_dwg_id']= $drawing['temp_dwg_id'];
    $test[$index]['temp_dwg_rev']= $drawing['temp_dwg_rev'];
    $test[$index]['project_no']= $project_no;
    $test[$index]['client_id']= $client['client_id'];
  }
}

编辑以显示使用array_push()的示例

foreach($dist as $client)
{
  foreach($drawings as $drawing)
  {
    $newDrawing = array(
      'temp_dwg_id'  => $drawing['temp_dwg_id'],
      'temp_dwg_rev' => $drawing['temp_dwg_rev'],
      'project_no'   => $project_no,
      'client_id'    => $client['client_id'],
    );
    array_push($test, $newDrawing);
  }
}

相关内容

  • 没有找到相关文章

最新更新