2

I am new to PHP.

I want to convert this array [["14785"],["125478"]] to string like this 14785,125478.

What is the way to do this?

Adishri Kulkarni
  • 135
  • 1
  • 1
  • 6

3 Answers3

6

try this,

Using implode() - Demo

$array = ["14785"],["125478"]
$str = implode(',',$array);
echo $str;

Using join() - Demo

$array =  ["14785","125478"];
$str = join(",",$array);
echo $str;

EDIT

For multi-dimention array, - Demo

$arr = [["14785"],["125478"]];
$str = implode(',', array_map(function($el){ return $el[0]; }, $arr));

echo $str;
Niranjan N Raju
  • 12,047
  • 4
  • 22
  • 41
5

use php's implode function

$arr =  ["14785","125478"];
echo implode(",",$arr);
Milan Maharjan
  • 4,156
  • 1
  • 21
  • 28
  • Without knowing the array structure it may not work, but for normal arrays this is the way.. – Svetoslav Dec 07 '15 at 11:13
  • join is faster than implode: http://dev.airve.com/demo/speed_tests/php/join_vs_implode.php `echo join(",",$arr);` – Thamilhan Dec 07 '15 at 11:14
  • 1
    @MyWay - `join()` is simply an alias for `implode()` so why do you believe that it's faster based on that arbitrary test page that you've linked..... the executed code is the same – Mark Baker Dec 07 '15 at 11:19
  • Mentioned what I came across – Thamilhan Dec 07 '15 at 11:21
  • The link you came across is a pretty arbitrary tester that isn't consistent from one run to the next, so it can't be taken as absolute gospel truth.... it's meaningless – Mark Baker Dec 07 '15 at 11:23
  • func2() took 1.014 times longer than func1(), func2() took 0.979 times longer than func1(), func2() took 1.003 times longer than func1(), func2() took 1.702 times longer than func1(), func2() took 0.966 times longer than func1() – Mark Baker Dec 07 '15 at 11:26
0

Using implode()

 <?php
$arr = array('Hello','World!','Beautiful','Day!');
echo implode(" ",$arr);
?>
Nimesh Patel
  • 796
  • 1
  • 7
  • 23