php将数组转为多维数组

发布于 2024-12-10 20:27:49 字数 1471 浏览 0 评论 0原文

我有一个(动态)数组,在本例中包含 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 脚本添加。 非常感谢任何提示。

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

人海汹涌 2024-12-17 20:27:49

这不是您问题的准确答案,但是...

您可以在 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' => '...',
    ),
)

This is not an exacty answer to you question, but...

You can use arrays in POST variables like so:

<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' => '...',
    ),
)
多情出卖 2024-12-17 20:27:49

试试这个:

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

)

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

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.

冰之心 2024-12-17 20:27:49

试试这个:

$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

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
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文