3

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
Cœur
  • 37,241
  • 25
  • 195
  • 267
Manthan Dave
  • 2,137
  • 2
  • 17
  • 31
  • this may helps http://stackoverflow.com/questions/16710800/implode-data-from-a-multi-dimensional-array – LF00 May 04 '17 at 08:34

1 Answers1

4

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
RomanPerekhrest
  • 88,541
  • 4
  • 65
  • 105