Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 672
maniclorn Avatar asked Jul 25 '11 13:07

maniclorn


People also ask

Which method is used for converting array into string?

JavaScript Array toString() The toString() method returns a string with array values separated by commas. The toString() method does not change the original array.

Can you convert an array to a string?

We can use Arrays. toString method that invoke the toString() method on individual elements and use StringBuilder to create String. We can also create our own method to convert String array to String if we have some specific format requirements.

Which method is used to convert arrays to strings in JavaScript?

The toString() method is used for converting and representing an array into string form. It returns the string containing the specified array elements.

How do you convert a number array to string?

To convert an array of numbers to an array of strings, call the map() method on the array, and on each iteration, convert the number to a string. The map method will return a new array containing only strings. Copied! const arrOfNum = [1, 2, 3]; const arrOfStr = arrOfNum.


1 Answers

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";} 
like image 118
Michael Berkowski Avatar answered Sep 21 '22 02:09

Michael Berkowski