Home Tutorial Php Phpbasics PHP Array to String Conversion

 
 

PHP Array to String Conversion
Posted on: October 24, 2009 at 12:00 AM
Advertisement
PHP Array to String Conversion: In this PHP tutorial you will learn how to convert an array to string. PHP provides implode() function which helps us to convert array to string. The join() function is an alias of implode() function. Examples will illustrates these functions.

PHP Array to String Conversion

Sometimes data arrive in array which has to be convert into string, in this situation we can use implode() function which is used to convert the array into string.

In PHP, conversion of an array to string is done by implode() function. The implode() function puts the separator (In general we use: +  , or any other special symbol) between each value of the array. This function joins array elements with a string.

General format of the implode() function is:

i)    string implode ( $separator, $array)

ii)    string implode (  $array)

iii)    string implode ( $array, $separator)

One important point about implode function is that it accepts its parameter in either order, if you don't pass any separator, even any space, PHP wont generate any warning or error message.

PHP Array to String Conversion Example 1:

<?php

$array1=$array2=array("CPU","VDU","KEYBOARD");

$array1=implode($array1,",");

print_r($array1);

echo"<br/>";

$array1=implode("+",$array2);

print_r($array1);

?>

Output:

CPU,VDU,KEYBOARD
CPU+VDU+KEYBOARD

join() function is an alias for implode(), output will be same as implode() function, example of join() is as follows:

Convert Array to String in PHP Example 2:

<?php

$array1=$array2=array("CPU","VDU","KEYBOARD");

$array1=join($array1,",");

print_r($array1);

echo"<br/>";

$array1=join("+",$array2);

print_r($array1);

?>

Output:

CPU,VDU,KEYBOARD
CPU+VDU+KEYBOARD

 

Advertisement

Related Tags for PHP Array to String Conversion:


Follow us on Twitter, or add us on Facebook or Google Plus to keep you updated with the recent trends of Java and other open source platforms.

Posted on: October 24, 2009

Recommend the tutorial

Advertisements Advertisements
 

 

 

DMCA.com