You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.17 13:23:22