duanqiongchong0354 2011-10-18 00:51
浏览 26
已采纳

php将数组转换为多维数组

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.

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. any hint much appreciated.

  • 写回答

3条回答 默认 最新

  • dqch34769 2011-10-18 01:20
    关注

    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:

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 Python3.5 相关代码写作
  • ¥20 测距传感器数据手册i2c
  • ¥15 RPA正常跑,cmd输入cookies跑不出来
  • ¥15 求帮我调试一下freefem代码
  • ¥15 matlab代码解决,怎么运行
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)