Terry Babu Samuel
Terry Babu Samuel

Reputation: 119

how to implode multidimensional array

Array
(
    [0] => Array
        (
            [0] => Array
                (
                    [name] => Attributes 2
                )

            [1] => Array
                (
                    [name] => Attributes 3
                )
        )
    [1] => Array
        (
            [0] => Array
                (
                    [name] => Attributes 1
                )

            [1] => Array
                (
                    [name] => Attributes 3
                )
        )
)

I want its result

Attributes 2, Attributes 3

Attributes 1, Attributes 3

Upvotes: 1

Views: 78

Answers (2)

Murad Hasan
Murad Hasan

Reputation: 9583

By using foreach and array_column you can did this thing. let array as $arr and use implode() for comma separated output.

Check this online: https://3v4l.org/Bdgh5

foreach($arr as $value){
    echo implode(", ", array_column($value, 'name'));
}

Let me know is it okey or not?

Upvotes: 1

KARTHI SRV
KARTHI SRV

Reputation: 499

It is another way, by using only foreach

$arr = array (
   0 => array (
        0 => array ( "name" => "Attributes 2"),
        1 => array ( "name" => "Attributes 3")
      ),
   1 => array ( 
        0 => array ( "name" => "Attributes 1"),
        1 => array ( "name" => "Attributes 3")
      )
);

foreach($arr as $value){
  foreach ($value as $key ) {
    echo $key['name'];
  }
}

Upvotes: 0

Related Questions