简体   繁体   中英

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 ?

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

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

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

With loop:

$array = array(
    0 => array(
        'name' => 'John Doe',
        'email' => 'john@example.com'
    ),
    1 => array(
        'name' => 'Jane Doe2',
        'email' => 'jane@example.com2'
    ),
);


//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,john@example.com,Jane Doe2,jane@example.com2

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM