首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >从多维关联后数组-代码点火器创建关联数组

从多维关联后数组-代码点火器创建关联数组
EN

Stack Overflow用户
提问于 2015-08-05 12:27:23
回答 1查看 483关注 0票数 0

我已经创建了一个生成多维post数组的表单。我想要更改为数组,这样就可以将它插入到我的表中。

这是返回的post数组。

代码语言:javascript
复制
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
                )

        )

)

因此,我认为该数组应该如下所示,以使查询正常工作

代码语言:javascript
复制
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是自动递增的。

代码语言:javascript
复制
|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的数组。我仍然需要将客户端数组添加到

代码语言:javascript
复制
        $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;

        }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2015-08-05 12:52:37

试试看:

代码语言:javascript
复制
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()的示例

代码语言:javascript
复制
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);
  }
}
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/31832511

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档