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

时间:2015-08-05 12:27:24

标签: php mysql codeigniter multidimensional-array

我创建了一个生成多维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
    )
)

我在db中的表看起来像这样。注意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;

        }

1 个答案:

答案 0 :(得分:0)

尝试一下:

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);
  }
}