对数组元素中的数据值进行分组

问题描述 投票:0回答:2

我有一个包含这样的表单字段的表单:

<form action="versions000003.php" method="post">
    <input type='hidden' name='method' value='recalculate' />
    <table>
        <tr>
        <td>1</td>
        <td><input type='checkbox' id='thread_1|1' name='add[]' value='1|1' /></td>
        <!-- ... increment to 43 -->
        <td><input type='checkbox' id='thread_1|43' name='add[]' value='1|43' /></td>
        </tr>
        <tr>
        <td>2</td>
        <td><input type='checkbox' id='thread_2|1' name='add[]' value='2|1' /></td>
        <!-- ... increment to 43 -->
        <td><input type='checkbox' id='thread_2|43' name='add[]' value='2|43' /></td>
        </tr>
        <td>3</td>
        <td><input type='checkbox' id='thread_3|1' name='add[]' value='3|1' /></td>
        <!-- ... increment to 43 -->
        <td><input type='checkbox' id='thread_3|43' name='add[]' value='3|43' /></td>
        </tr>
    </table>
    <button type='submit' class='btn btn-danger'>Go</button>
</form>

提交表格后,我会这样做:

if ($_SERVER['REQUEST_METHOD'] == 'POST') {

    $mode = $_POST['method'];

    if ($mode = "ReCalculate") {

        if (is_array($_POST['add'])) {

            print_r($_POST['add']);

            foreach ($_POST as $key => $value)
             print_r($value);

        }

    }

}

生成的数组,根据所选内容,可能如下所示,例如:

Array
(
    [0] => 1|1
    [1] => 1|2
    [2] => 1|3
    [3] => 1|4
    [4] => 1|5
    [5] => 1|6
    [6] => 1|32
    [7] => 1|35
    [8] => 2|1
    [9] => 2|5
    [10] => 3|1
    [11] => 3|8
    [12] => 3|11
    [13] => 3|13
    [14] => 3|35
)

我想弄清楚如何更改表单处理逻辑,最终在本例中数组中有 3 个元素。我想按管道之前的值对管道之后的值进行分组 - 所以最终得到:

Array
(
    [0] => 1|1,2,3,4,5,6,32,35
    [1] => 2|1,5
    [2] => 3|1,8,11,13,25
)

这可能吗?如果可以,请就如何实现该结果提供任何建议,我将不胜感激。谢谢

php arrays
2个回答
1
投票

使用

$_POST
分隔符循环遍历
explode
|

$array = array('1|1','1|2','2|1','2|5','3|1','3|8');
//simplified version of your array

$new_array = array();
//define a new array  that will be built in the loop below in your desired format

foreach($array as $item){ //loop through original array

    $parsed_item = explode('|',$item); 
   //explode each item to split elements using the | as delimiter

    if(isset($new_array[$parsed_item[0]])){
         //if $new_array with the current key already exists add new value to existing array (concatenate with a comma to existing value)
        $new_array[$parsed_item[0]] = $new_array[$parsed_item[0]].','.$parsed_item[1];

    }else{ 
        //else create new value on array
        $new_array[$parsed_item[0]] = $parsed_item[0].'|'.$parsed_item[1];
    }
}

0
投票

用管道分割每个遇到的字符串。使用前缀来分组。每次遇到新组时,将组引用推送到结果数组中,并将整个输入字符串作为默认值。 当再次遇到组时,仅附加输入字符串中的后缀(以逗号分隔)。 这将直接产生所需的索引结果数组。 演示

$result = [];
foreach ($array as $v) {
    [$group, $value] = explode('|', $v, 2);
    if (!isset($ref[$group])) {
        $ref[$group] = $v;
        $result[] =& $ref[$group];
    } else {
        $ref[$group] .= ",$value";
    }
}
var_export($result);

输出:

array (
  0 => '1|1,2,3,4,5,6,32,35',
  1 => '2|1,5',
  2 => '3|1,8,11,13,35',
)
© www.soinside.com 2019 - 2024. All rights reserved.