Wednesday, October 2, 2013

PHP Array to String and String to Array Conversion

Hiiii, we all know that a developer always need a functionality to change the array to string and string to array. We can write a code for this in any language with the help of some loops. But PHP offers two most important functions namely implode() and explode() for array to string and string to array respectively.
So, in this post I will show you the functionality of both of these functions.

PHP Array to String and String to Array Conversion


Implode() function :

This function converts Array to string separated by character which you passes as first parameter in implode() function, in our case we are making string with elements separated by "," (comma).
$array = array('first''second''third');
$stringimplode(","$array);        // this string will contain items seperated by ","
echo 
$string;     // will generate the output as first,second,third


Explode() function :

This function converts String to array. This array is formed by splitting the string to array with some special characters like "*","/","&"," ","-",etc used in explode() function. 
$String"one two three four five six";$arrayexplode(" "$String);  // string is divided to array elements by " " (space) between the elements. 
echo "<pre>";
print_r($array);
 //This will generate output as:
Array
(
    [0] => one
    [1] => two
    [2] => three
    [3] => four
    [4] => five
    [5] => six
)

Hope you like this post……..Please Comment…………!!!!!!!!!


Tags:

0 Responses to “PHP Array to String and String to Array Conversion”

Post a Comment

© 2013 MyCodeStock. All rights reserved.
Designed by SpicyTricks