1

I have an array POST after submited the form:

$POST = Array
(
    [29#487_Mon] => 90,
    [29#487_Mon_power] => 50,
    [30#487_Thur]=>80,
    [30#487_Thur_power]=>90,
    [41#487_Wed]=>10,
    [41#487_Wed_power]=>20,

)

And I want is transform the $POST array to more friendly format that look like this:

$Transform = array(
    [29]=>array(
            [487_Mon] => 90,
            [487_Mon_power] => 50
            ),
    [30]=>array(
            [487_Thur]=>80,
            [487_Thur_power]=>90
            ),
    [41]=>array(
            [487_Wed]=>10,
            [487_Wed_power]=>20
            )
    )

Anyone Know how to do this?

4

2 回答 2

4

正如@Gordon 在评论中指出的那样,另外,您可以通过以下方式完成:

<?php
$Transform = array();
foreach($POST as $key => $val){
 $key_parts = explode("#", $key); //spliting 29#487_Mon to 29 and 487_Mon
 /*
 $key_parts[0] = 29
 $key_parts[1] = 487_Mon
 */
 $Transform[$key_parts[0]][$key_parts[1]] = $val;
}
echo  '<pre>';
print_r($Transform);
echo  '</pre>';
?>

演示

于 2013-01-28T11:16:37.267 回答
2

这是在扩展@Gordon 所说的内容。如果@Gordon 希望提交他的评论作为答案,我将收回我的答案。

测试和工作

<?
    if( $_POST['submit'] ) {
        echo "<pre>";
        print_r( $_POST );
        echo "</pre>";
    }
?>

<form action="" method="post">
    <input type="text" name="29[487_Mon]"/>
    <input type="text" name="29[487_Mon_power]"/>
    <br/><br/>
    <input type="text" name="30[487_Thur]"/>
    <input type="text" name="30[487_Thur_power]"/>
    <br/><br/>
    <input type="text" name="41[487_Wed]"/>
    <input type="text" name="41[487_Wed_power]"/>
    <br/><br/>

    <input type="submit" name="submit" />
</form>
于 2013-01-28T11:21:29.670 回答