3

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 ?

3
  • Show your attempt with array_map(), you're probably not far from the solution. Commented Jul 25, 2016 at 13:10
  • array_map() will do it for you Commented Jul 25, 2016 at 13:11
  • @Rizier123 @SunilPachlangia I've updated my question with array_map() Commented Jul 25, 2016 at 13:14

4 Answers 4

5

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

Sign up to request clarification or add additional context in comments.

Comments

1

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

Comments

0

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

Comments

0

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]

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.