0

I have an array as shown bottom

array (
    [det1] => 1,2,3,4 
    [det2] => 5,6
);

So i want to join items of this array and convert array to an string like bottom

$uru = 1,2,3,4,5,6

How can i do this work?

Mohammad
  • 21,175
  • 15
  • 55
  • 84
Ram
  • 115
  • 8
  • If you need more advanced snippet, please go through [link](https://stackoverflow.com/questions/1319903/how-to-flatten-a-multidimensional-array) – Rahul Sep 11 '18 at 08:42
  • Possible Duplicate (assuming the title is correct and the input data is wrong): https://stackoverflow.com/q/1319903/2943403 – mickmackusa Sep 20 '18 at 00:29

3 Answers3

4

Did you try like this with implode()

<?php
$arr = array('det1'=>'1,2,3,4', 'det2'=>'5,6');
$uru = implode(',',$arr);
echo $uru;
?>

DEMO: https://3v4l.org/UBQrv

A l w a y s S u n n y
  • 36,497
  • 8
  • 60
  • 103
3

You say multidimensional array, but your array is single dimensional with strings?
I assume that is a typo and your array is multidimensional.

In that case loop the array and merge the new array with the subarray.

$arr = array (
    "det1" => [1,2,3,4],
    "det2" => [5,6]
);

$new= [];
foreach($arr as $sub){
    $new = array_merge($new, $sub);
}

echo implode(",",$new); // 1,2,3,4,5,6

https://3v4l.org/NaDXN

Andreas
  • 23,610
  • 6
  • 30
  • 62
0

This can be easily done by extracting into array_merge and then joining the resulting array.

$data = [
  [1,2,3,4,5],
  [6,7,8],
];

echo join(',', array_merge(... $data));

Output:

1,2,3,4,5,6,7,8
nhlm
  • 55
  • 5
  • Good idea, but this won't work because `$data` has string keys, and you can't unpack an array with string keys. It should work if you used `array_values` on `$data`. – Don't Panic Sep 20 '18 at 15:16