How to convert array to a string using methods other than JSON?

maniclorn picture maniclorn · Jul 25, 2011 · Viewed 238.4k times · Source

What is a function in PHP used to convert array to string, other than using JSON?

I know there is a function that directly does like JSON. I just don't remember.

Answer

Michael Berkowski picture Michael Berkowski · Jul 25, 2011

serialize() is the function you are looking for. It will return a string representation of its input array or object in a PHP-specific internal format. The string may be converted back to its original form with unserialize().

But beware, that not all objects are serializable, or some may be only partially serializable and unable to be completely restored with unserialize().

$array = array(1,2,3,'foo');
echo serialize($array);

// Prints
a:4:{i:0;i:1;i:1;i:2;i:2;i:3;i:3;s:3:"foo";}