Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Php - How to convert multiple key values array to | (Pipe) Separated string

I am working on one project with multiple array operations.

I have one variable called $product_attributes and it contains below array 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 into | (Pipe) Separated String like 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 as below:

OutputArray|Array|Array|Array|Array|Array
like image 550
Manthan Dave Avatar asked Sep 15 '26 02:09

Manthan Dave


1 Answers

The solution using array_map and implode functions:

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

print_r($result);

The output:

Applications=Steel; PVC; Std. Wall|Blade Exp.=0.29|Fits Model=153
like image 163
RomanPerekhrest Avatar answered Sep 16 '26 20:09

RomanPerekhrest



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!