2014-01-10 49 views
0

我有一个来自表单POST的数组,我想将它们组合在一起。使用表单域创建PHP数组

我的第一次尝试是在数组上运行一个foreach循环,但我没有成功将逻辑分组。我有用按键编号创建动态数组的想法,但我无法获取该数字。

初始阵列:

["affix-1-order"]=> "1"; 
["affix-1-type"]=> "Apple"; 
["affix-1-count"]=> "5"; 
["affix-3-order"]=> "2"; 
["affix-3-type"]=> "Orange"; 
["affix-3-count"]=> "10"; 
["affix-2-order"]=> "3"; 
["affix-2-type"]=> "Banana"; 
["affix-2-count"]=> "3"; 
["affix-4-order"]=> "4"; 
["affix-4-type"]=> "Mango"; 
["affix-4-count"]=> "15"; 

预期输出:

["1"]=> [{ 
    ["type"]=> "Apple", 
    ["count"]=> "5", 
    ["order"]=> "1" 
}], 
["2"]=> [{ 
    ["type"]=> "Banana", 
    ["order"]=> "3", 
    ["count"]=> "3" 
}], 
["3"]=> [{ 
    ["type"]=> "Orange", 
    ["order"]=> "2", 
    ["count"]=> "10", 
}], 
["4"]=> [{ 
    ["type"]=> "Mango", 
    ["order"]=> "4", 
    ["count"]=> "15" 
}]; 
+2

让你有在后所需的数组,你可以建立自己的形式进行任何操作上 – 2014-01-10 03:12:07

+0

后者我不明白?你如何建议我在表单输入中分组键和3个值? – veksen

+0

'name = X [] ['type']''name = X [] ['order']''name = X [] ['count']',帖子X数组将会是你想要的 – 2014-01-10 03:35:48

回答

2

我宁愿遵循达贡的建议和修改形式或发送所述数据Ajax请求。

通过在字段名称中使用方括号,您可以让PHP为您完成工作。

<?php 
if(count($_POST)) 
{ 
    print_r ($_POST['data']); 
    die(); 
} 
?> 

<body onload='document.getElementById("post").submit();'> 
    <form method="POST" id="post"> 
     <input type='text' name='data[0][type]' value='Apple'> 
     <input type='text' name='data[0][count]' value='5'> 
     <input type='text' name='data[0][order]' value='1'> 
     <input type='text' name='data[1][type]' value='Banana'> 
     <input type='text' name='data[1][count]' value='3'> 
     <input type='text' name='data[1][order]' value='3'> 
     <input type='text' name='data[2][type]' value='Orange'> 
     <input type='text' name='data[2][count]' value='2'> 
     <input type='text' name='data[2][order]' value='10'> 
     <input type='text' name='data[3][type]' value='Mango'> 
     <input type='text' name='data[3][count]' value='4'> 
     <input type='text' name='data[3][order]' value='15'> 
    </form> 
</body> 

输出

[0] => Array 
     [type] => Apple 
     [count] => 5 
     [order] => 1 
[1] => Array 
     [type] => Banana 
     [count] => 3 
     [order] => 3 
[2] => Array 
     [type] => Orange 
     [count] => 2 
     [order] => 10 
[3] => Array 
     [type] => Mango 
     [count] => 4 
     [order] => 15 
+0

没有足够的阅读,看到这是来自'POST'形式。这将是更好的选择。 – Sam

+0

@SamSullivan OTOH我的解决方案达不到正则表达式:) –

+0

我不知道这是可能的......谢谢你,你只是救了我几个小时的工作:) – veksen

3
$array = array(
    "affix-1-order" => "1", 
    "affix-1-type" => "Apple", 
    "affix-1-count" => "5", 
    "affix-3-order" => "2", 
    "affix-3-type" => "Orange", 
    "affix-3-count" => "10", 
    "affix-2-order" => "3", 
    "affix-2-type" => "Banana", 
    "affix-2-count" => "3", 
    "affix-4-order" => "4", 
    "affix-4-type" => "Mango", 
    "affix-4-count" => "15", 
); 

$final = array(); 
foreach($array as $key => $value) { 
    preg_match('/affix\-([\d]+)\-(.*)/', $key, $matches); 
    // $matches[0] = 'affix-1-order'; 
    // $matches[1] = '1'; 
    // $matches[2] = 'order'; 

    $final[$matches[1]][$matches[2]] = $value; 
} 

print_r($final); 
// Array (
// [1] => Array ([order] => 1 [type] => Apple [count] => 5) 
// [3] => Array ([order] => 2 [type] => Orange [count] => 10) 
// [2] => Array ([order] => 3 [type] => Banana [count] => 3) 
// [4] => Array ([order] => 4 [type] => Mango [count] => 15) 
//) 
+0

我结束了修改表单,但这是一个很棒的解决方案,谢谢! :) – veksen

+0

没问题@veksen,修改表单是一个更好的解决方案。但它总是很好学习一点正则表达式;) – Sam

+1

正则表达式是definitly我的下一个大跃进 – veksen