简体   繁体   English

php将数组转换为多维数组

[英]php turning array into multi-dimensional array

i have a (dynamic) array, which in this example contains 4 sets of data (5 fields per set), but it could be only one set or up to 25 sets. 我有一个(动态)数组,在这个例子中包含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] => ) 

which i'd like to turn into something like 我想变成类似的东西

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] => )
        )

the original array is created this way: 以这种方式创建原始数组:

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

the field name digit is already added with JS before form submission, and not by php script. 表单提交之前,字段名称数字已经与JS一起添加,而不是由php脚本添加。 any hint much appreciated. 任何提示,不胜感激。

This is not an exacty answer to you question, but... 这不是你问题的确切答案,但......

You can use arrays in POST variables like so: 您可以在POST变量中使用数组,如下所示:

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

will give you: 会给你:

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

Try this: 尝试这个:

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

given an $original_array equal to described, will produce $new_array: 如果$ 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] => 
        )

)

I was uncertain about how much you knew about the elements or their order, so this code basically takes any collection of elements that end in numbers and rearranges them in groups that have the same ending number. 我不确定你对元素或它们的顺序了解多少,所以这段代码基本上采用以数字结尾的任何元素集合,并将它们重新排列在具有相同结尾编号的组中。

Try this: 尝试这个:

$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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM