Php - How to convert an array of values ​​of multiple keys to | (Trumpet) Split string

I am working on one project with multiple array operations.

I have one variable named $ product_attributes and it contains below value as value.

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
        )
)

      

Now I want to convert it to | (Pipe) Separated String as below:

Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153

      

Below is what I have tried:

$tags = implode('|',$product_attributes);
echo "Output".$tags;

      

But it returns output like below:

OutputArray|Array|Array|Array|Array|Array

      

+3


source to share


1 answer


Solution using functions array_map

and implode

:

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

print_r($result);

      



Output:

Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153

      

+4


source







All Articles