使用表单字段创建PHP数组

时间:2014-01-10 03:07:03

标签: php arrays

我有一个来自表单POST的数组,我想将它们组合在一起。

我的第一次尝试是在阵列上运行一个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 个答案:

答案 0 :(得分: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 )
// )

答案 1 :(得分:2)

我宁愿遵循Dagon的建议并修改发送数据的表单或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