从多个数组创建数组&在mysql表中提交

时间:2017-04-29 15:16:26

标签: php arrays multidimensional-array

我正在尝试从少数人那里获取值 - 动态创建的HTML FORM&在动作文件中,我通过$ _POST获取这些值。请谦卑地考虑一下,我在动态输入中使用数组,如:

<input name="array1[]" />
<input name="array2[]" />
<input name="array3[]" />

那么,在FORM SUBMIT之后,在动作文件中,它给出了:

         $a =    Array
        (
            [0] => 04/21/2017
            [1] => 04/19/2017
            [2] => 04/25/2017
            [3] => 04/25/2017
            [4] => 10/25/2017
        )

    $b=    Array
        (
            [0] => 11
            [1] => 34
            [2] => 12
            [3] => 12
            [4] => 2
        )

      $c=  Array
        (
            [0] => fghgthg
            [1] => ggfg
            [2] => fgfgfdgf
            [3] => fgfdgdgfgdh
            [4] => rgrgfgf
        )

现在,安排这样的麻烦:

$FinalArray = array(

array($a[0], $b[0], $c[0]),
array($a[1], $b[1], $c[1]),
array($a[2], $b[2], $c[2]),
array($a[3], $b[3], $c[3]),
......last line without comma

  );

并在mysql表中提交,以便我可以轻松地检索它:

step-1 : $FinalArray[0];
step-2 : $FinalArray[1]; 
.......goes on

先谢谢你的帮助。

1 个答案:

答案 0 :(得分:1)

这个简单的foreach可以帮助您实现所需的输出。

Try this code snippet here

<?php

ini_set('display_errors', 1);

$a = Array
    (
    0 => "04/21/2017",
    1 => "04/19/2017",
    2 => "04/25/2017",
    3 => '04/25/2017',
    4 => "10/25/2017"
);

$b = Array
    (
    0 => 11,
    1 => 34,
    2 => 12,
    3 => 12,
    4 => 2
);

$c = Array
    (
    0 => "fghgthg",
    1 => "ggfg",
    2 => "fgfgfdgf",
    3 => "fgfdgdgfgdh",
    4 => "rgrgfgf",
);
$result=array();
foreach($a as $key => $value)
{
    $result[]=array($value,$b[$key],$c[$key]);
}
print_r($result);

<强>输出:

Array
(
    [0] => Array
        (
            [0] => 04/21/2017
            [1] => 11
            [2] => fghgthg
        )

    [1] => Array
        (
            [0] => 04/19/2017
            [1] => 34
            [2] => ggfg
        )

    [2] => Array
        (
            [0] => 04/25/2017
            [1] => 12
            [2] => fgfgfdgf
        )

    [3] => Array
        (
            [0] => 04/25/2017
            [1] => 12
            [2] => fgfdgdgfgdh
        )

    [4] => Array
        (
            [0] => 10/25/2017
            [1] => 2
            [2] => rgrgfgf
        )

)