php将数组转换为多维数组

时间:2011-10-18 00:51:03

标签: php arrays loops multidimensional-array

我有一个(动态)数组,在这个例子中包含4组数据(每组5个字段),但它可能只有一组或最多25组。

Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => ) 

我想变成像

这样的东西
Array ( Array ( [lightwattage1] => 50 [lightvoltage1] => 12 [lightquantity1] => 2 [lightusage1] => 4 [lightcomment1] => ),
        Array ( [lightwattage2] => 60 [lightvoltage2] => 24 [lightquantity2] => 4 [lightusage2] => 5 [lightcomment2] => ),
        Array ( [lightwattage3] => 30 [lightvoltage3] => 240 [lightquantity3] => 4 [lightusage3] => 2 [lightcomment3] => ), 
        Array ( [lightwattage4] => 25 [lightvoltage4] => 12 [lightquantity4] => 2 [lightusage4] => 6 [lightcomment4] => )
        )

以这种方式创建原始数组:

$light = Array();

foreach( $_POST as $key => $val )
{
//field names that start with light to go in this array
    if (strpos($key, 'light') === 0) {
        $light[$key] = $val;
    }
}

字段名称数字已经在表单提交之前添加了JS,而不是通过php脚本添加。 任何提示非常赞赏。

3 个答案:

答案 0 :(得分:2)

这不是你问题的确切答案,但是......

您可以在POST变量中使用数组,如下所示:

<input name="light[1][wattage]" />
<input name="light[1][voltage]" />
<input name="light[2][wattage]" />
<input name="light[2][voltage]" />

会给你:

$_POST['light'] == array(
    1 => array(
        'wattage' => '...',
        'voltage' => '...',
    ),
    2 => array(
        'wattage' => '...',
        'voltage' => '...',
    ),
)

答案 1 :(得分:1)

试试这个:

$prefixes = array();
$postfixes = array();
foreach($original_array as $key=>$value)
{
        preg_match('/^([^\d]*)(\d+)$/',$key,$matches);
        if(count($matches)>1)
        {
                if(!in_array($matches[1], $prefixes)) $prefixes[] = $matches[1];
                if(!in_array($matches[2], $postfixes)) $postfixes[] = $matches[2];
        }
}   
$new_array = array();
foreach($postfixes as $postfix)
{
        $new_element = array();
        foreach($prefixes as $prefix)
        {
                if(isset($original_array[$prefix.$postfix])) $new_element[$prefix.$postfix] = $original_array[$prefix.$postfix];
        }
        $new_array[] = $new_element; 
}

给定$ original_array等于描述,将产生$ new_array:

Array
(
    [0] => Array
        (
            [lightwattage1] => 50
            [lightvoltage1] => 12
            [lightquantity1] => 2
            [lightusage1] => 4
            [lightcomment1] => 
        )

    [1] => Array
        (
            [lightwattage2] => 60
            [lightvoltage2] => 24
            [lightquantity2] => 4
            [lightusage2] => 5
            [lightcomment2] => 
        )

    [2] => Array
        (
            [lightwattage3] => 30
            [lightvoltage3] => 240
            [lightquantity3] => 4
            [lightusage3] => 2
            [lightcomment3] => 
        )

    [3] => Array
        (
            [lightwattage4] => 25
            [lightvoltage4] => 12
            [lightquantity4] => 2
            [lightusage4] => 6
            [lightcomment4] => 
        )

)

我不确定你对元素或它们的顺序了解多少,所以这段代码基本上采用以数字结尾的任何元素集合,并将它们重新排列在具有相同结尾编号的组中。

答案 2 :(得分:0)

试试这个:

$outarr = array()
$subarr = array()
$i=0;
foreach($_POST as $key => $val)
{
    //only include keys starting with light:
    if (strpos("light", $key)==0)
    {
        //create a new subarray each time we find a key that starts with "lightwattage":
        if ($i>0 && strpos("lightwattage", $key)==0)
        {
            $outarr[] = $subarr;
        }
        $subarr[$key] = $val;
        $i++;
    }
}
$outarr[] = $subarr;

//$outarr contains what you want here