I created a array using PHP
$userarray = array('UserName'=>$username,'UserId'=>$userId,'UserPicURL'=>$userPicURL);
How can I convert this array into a string in PHP and back from string into an array. This is kind of a requirement. Could someone please advice on how this can be acheived.
Definition and UsageThe implode() function returns a string from the elements of an array. Note: The implode() function accept its parameters in either order. However, for consistency with explode(), you should use the documented order of arguments. Note: The separator parameter of implode() is optional.
toString() The toString() method returns a string representing the specified array and its elements.
Maybe in case you need to convert a multidimensional array into a string, you may want to use the print_r() function. This is also called “array with keys”. By adding “true” as its second parameter, all the contents of the array will be cast into the string.
You can convert any PHP data-type but resources into a string by serializing it:
$string = serialize($array);
And back into it's original form by unserializing it again:
$array = unserialize($string);
A serialized array is in string form. It can be converted into an array again by unserializing it.
The same does work with json_encode
/ -_decode
for your array as well:
$string = json_encode($array);
$array = json_decode($string);
use the function implode(separator,array) which return a string from the elements of an array.
and then the function explode ( string $delimiter , string $string [, int $limit ] ) to revert it back to an array
$array_as_string = implode(" ",$userarray);
$new_array = explode(" ",$array_as_string);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With