PHP按sequence_answer_id列分组二维数组转换结构实现问题
PHP按指定列对数组行分组实现
需求
开发中需要按sequence_answer_id列值对二维数组行分组,忽略form_id字段,将相同分组下的column_name作为键、column_value作为值存入同一分组数组,最终得到索引从0开始的结果数组。
输入数据
$tbldocumentacao_eleitoral_answers = array( array('id' => '1', 'sequence_answer_id' => '4', 'column_name' => 'form_id', 'column_value' => '3'), array('id' => '2', 'sequence_answer_id' => '4', 'column_name' => 'text-1625344899360', 'column_value' => '1000'), array('id' => '3', 'sequence_answer_id' => '4', 'column_name' => 'text-1625346513466', 'column_value' => 'Text 2'), array('id' => '4', 'sequence_answer_id' => '4', 'column_name' => 'text-1625346520950', 'column_value' => 'Text 3'), array('id' => '5', 'sequence_answer_id' => '4', 'column_name' => 'text-1625346541360', 'column_value' => 'VALUE ANY'), array('id' => '9', 'sequence_answer_id' => '5', 'column_name' => 'form_id', 'column_value' => '3'), array('id' => '10', 'sequence_answer_id' => '5', 'column_name' => 'text-1625344899360', 'column_value' => '1080'), array('id' => '11', 'sequence_answer_id' => '5', 'column_name' => 'text-1625346513466', 'column_value' => 'VALUE Any'), array('id' => '12', 'sequence_answer_id' => '5', 'column_name' => 'text-1625346520950', 'column_value' => 'taffarelxavier7@gmail.com'), array('id' => '13', 'sequence_answer_id' => '5', 'column_name' => 'text-1625346541360', 'column_value' => '63999480630'), array('id' => '17', 'sequence_answer_id' => '6', 'column_name' => 'form_id', 'column_value' => '3'), array('id' => '18', 'sequence_answer_id' => '6', 'column_name' => 'text-1625344899360', 'column_value' => '7895'), array('id' => '19', 'sequence_answer_id' => '6', 'column_name' => 'text-1625346513466', 'column_value' => 'IARA'), array('id' => '20', 'sequence_answer_id' => '6', 'column_name' => 'text-1625346520950', 'column_value' => 'taffa@gmail.com'), array('id' => '21', 'sequence_answer_id' => '6', 'column_name' => 'text-1625346541360', 'column_value' => '63999484570'), );
预期输出
Array ( [0] => Array ( [sequence_answer_id] => 4 [text-1625344899360] => 1000 [text-1625346513466] => Text 2 [text-1625346520950] => Text 3 [text-1625346541360] => VALUE ANY ) [1] => Array ( [sequence_answer_id] => 5 [text-1625344899360] => 1080 [text-1625346513466] => VALUE Any [text-1625346520950] => taffarelxavier7@gmail.com [text-1625346541360] => 63999480630 ) [2] => Array ( [sequence_answer_id] => 6 [text-1625344899360] => 7895 [text-1625346513466] => IARA [text-1625346520950] => taffa@gmail.com [text-1625346541360] => 63999484570 ) )
现有代码问题
原有代码错误点如下:
- 赋值时错误将
column_name作为值写入,没有取column_value - 每次都追加新数组到分组下,没有直接将键值对合并到当前分组数组,导致嵌套层级错误
- 没有过滤不需要的
form_id字段 - 最终外层数组键为
sequence_answer_id,不是预期的从0开始的连续索引
正确实现代码
$result = []; foreach ($tbldocumentacao_eleitoral_answers as $item) { $sid = $item['sequence_answer_id']; // 过滤不需要保留的form_id字段 if ($item['column_name'] === 'form_id') { continue; } // 首次遇到对应分组时初始化 if (!isset($result[$sid])) { $result[$sid] = [ 'sequence_answer_id' => $sid ]; } // 写入键值对 $result[$sid][$item['column_name']] = $item['column_value']; } // 重置外层数组键为连续索引 $result = array_values($result); // 输出验证 print_r($result);
内容的提问来源于stack exchange,提问作者Taffarel Xavier
相关产品推荐
相关产品推荐

