PHP中如何对数组进行分组与拼接?
数组分组拼接实现方案
当然可以实现这种对应位置元素的拼接操作,以下是基于常见编程语言的实现示例:
原数组结构(PHP 格式)
$test = [ ['one', 'two', 'three'], ['2', '2098', '2086'] ];
PHP 实现代码
$result = []; // 按索引对应拼接两个子数组的元素 foreach ($test[0] as $index => $text) { $result[0][] = $text . ', ' . $test[1][$index]; } // 输出目标结构 print_r(['test' => $result]);
输出结果
[test] => Array ( [0] => Array ( [0] => one, 2 [1] => two, 2098 [2] => three, 2086 ) )
JavaScript 实现代码
如果用 JavaScript 处理,逻辑类似:
const test = [ ['one', 'two', 'three'], ['2', '2098', '2086'] ]; const result = [[ `${test[0][0]}, ${test[1][0]}`, `${test[0][1]}, ${test[1][1]}`, `${test[0][2]}, ${test[1][2]}` ]]; console.log({ test: result });
内容的提问来源于stack exchange,提问作者felix manzanas
相关产品推荐
相关产品推荐

