Php - 如何将多个键值对数组转换为 |(管道)分隔的字符串

3

我正在处理一个包含多个数组操作的项目。

我的一个变量叫做$product_attributes,它包含以下数组作为其值。

Array
(
    [0] => Array
        (
            [0] => Applications
            [1] => Steel; PVC; Std. Wall
        )

    [1] => Array
        (
            [0] => Blade Exp.
            [1] => 0.29
        )

    [2] => Array
        (
            [0] => Fits Model
            [1] => 153
        )
)

现在我想将它转换为|(管道)分隔的字符串,如下所示:
Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153

以下是我尝试过的内容:
$tags = implode('|',$product_attributes);
echo "Output".$tags;

但它会返回以下输出:
OutputArray|Array|Array|Array|Array|Array

尝试使用以下方法从多维数组中合并数据 https://dev59.com/VWQn5IYBdhLWcg3wmIHY - LF00
1个回答

4

使用 array_mapimplode 函数的解决方案:

$result = implode("|", array_map(function ($v) {
    return $v[0] . "=" .$v[1];
}, $product_attributes));

print_r($result);

输出结果:
Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接