Sébastien Gicquel
Sébastien Gicquel

Reputation: 4386

Implode data from a Two-dimensional array

I think it should not be so difficult to solve but i can’t get the solution.

I’m trying to implode an array in order to have this result :

$new_array = "755, 646, 648, 260, 257, 261, 271, 764, ..."

Here is my array :

Array
(
    [0] => Array
        (
            [0] => 755
            [1] => 646
        )

    [1] => Array
        (
            [0] => 648
            [1] => 260
            [2] => 257
            [3] => 261
        )

    [2] => Array
        (
            [0] => 271
        )

    [3] => Array
        (
            [0] => 764
            [1] => 643
            [2] => 260
            [3] => 263
            [4] => 756
            [5] => 763
            [6] => 762
            [7] => 259
            [8] => 257
            [9] => 758
            [10] => 261
            [11] => 768
            [12] => 757
            [13] => 647
        )
)

I know how to do this with a simple array :

$newarray = implode(", ", $array);

I know how to get the key (with array_keys)

$new_array = implode(", ", array_keys($array));

I’ve tried with array_map but without result for the moment.

Coud you help me and point me in the right direction ?

Upvotes: 3

Views: 1607

Answers (4)

Atanas Atanasov
Atanas Atanasov

Reputation: 211

With loop:

$array = array(
    0 => array(
        'name' => 'John Doe',
        'email' => '[email protected]'
    ),
    1 => array(
        'name' => 'Jane Doe2',
        'email' => '[email protected]'
    ),
);


//function to display values
function ImplodeDataValue($array) {
$tosinglearray=array();
for ($i = 0; $i <= count($array)-1; $i++) {
$tosinglearray[]=implode(',',$array[$i]);
}
return implode(',',$tosinglearray);
}

echo ImplodeDataValue($array);
//returns John Doe,[email protected],Jane Doe2,[email protected]

Upvotes: 0

RomanPerekhrest
RomanPerekhrest

Reputation: 92854

Simple solution using array_walk_recursive function:

// $arr is your initial array
$new_arr = [];
array_walk_recursive($arr, function($v) use(&$new_arr){ $new_arr[] = $v; });

DEMO link

http://php.net/manual/en/function.array-walk-recursive.php

Upvotes: 1

lazyCoder
lazyCoder

Reputation: 2561

try this:

<?php 
      $array = array
      (
      array(755,646),
      array(648,260,257,261),
      array(271),
      array(764,643,260,263,756,763,762,259,257,758,261,768,757,647)
      );
      echo "<pre>";
      print_r($array);
      foreach ($array as $value) {
        $newstring[]=implode(",",$value);
      }
      echo implode(",",$newstring);

    ?>

You can do it with foreach loop like above

Upvotes: 0

George G
George G

Reputation: 7695

You have to merge inner arrays to one (if our multidimensional array is called $list), we can:

$merged = array_reduce($list, 'array_merge', array());

now you can easily implode it:

$string = implode(', ', $merged);

echoing given string would print:

755, 646, 648, 260, 257, 261, 271, 764, ...

To know you can't implode multidimensional array because it's elements are arrays itself so array to string conversion error may arise, thus we should make our array flat, for this purpose you can also check How to Flatten a Multidimensional Array?.

Also to know what array_reduce do is:

Iteratively reduce the array to a single value using a callback function.. see more

Upvotes: 5

Related Questions