View Discussion Show Improve Article Save Article View Discussion Improve Article Save Article We have given an array and the task is to convert the array elements into string. In this article, we are using two methods to convert array to string. Method 1: Using implode() function: The implode() method is an inbuilt function in PHP and is used to join the elements of an array. The implode() method is an alias for PHP | join() function and works exactly same as that of join() function. Syntax: string implode($separator, $array) Example: PHP
Output: Welcome to GeeksforGeeks A Computer Science Portal Method 2:
Using json_encode() Function:The json_encode() function is an inbuilt function in PHP which is used to convert PHP array or object into JSON representation. string json_encode( $value, $option, $depth ) Example: PHP
Output: {"name":"GFG","0":{"email":"","mobile":"XXXXXXXXXX"}} PHP is a server-side scripting language designed specifically for web development. You can learn PHP from the ground up by following this PHP Tutorial and PHP Examples. This code explains how to convert an array into a string in PHP using implode function. There are two ways to convert an array into a string in PHP.
Using implode() FunctionBy using the implode() function, we can convert all array elements into a string. This function returns the string. The separator parameter in implode() function is optional. But it is good to use two arguments. Syntax
Example
In the above example, at first line, an array variable is declared and assigned some values. In the next line, the implode() function will convert the array into a string. Two parameters are passed in the implode() function. First is the separator and the other one is array_name. Output You can also convert that string into an array if required. For that, we can use PHP's explode() function. explode() functionBy using explode() function, we can convert a string into array elements. We can pass three arguments in it. The first one is for separator, second for array_name, and the last one for the limit. Syntax
Example
In the above example, a string variable is assigned some value. Then, explode() function breaks this string into an array. After this, we used print_r() function which prints all the array elements and its indexes. Output Using json() FunctionIn PHP, objects can be converted into JSON String by using the PHP function json_encode(). A common use of JSON is to read data from a web server and display the data on a web page. Syntax
Example
In the above example, we assigned the value to the object variable and then in json_encode()converts the value to the array variable and makes the associative array. Output How do I turn an array into a string in PHP?There are two ways to convert an array into a string in PHP.. Using implode() function.. Using json_encode() function.. What is the name of function used to convert an array into a string in PHP?The implode() function returns a string from the elements of an array. Note: The implode() function accept its parameters in either order.
Which method converts an array to string?Arrays.
toString() method is used to return a string representation of the contents of the specified array. The string representation consists of a list of the array's elements, enclosed in square brackets (“[]”). Adjacent elements are separated by the characters “, ” (a comma followed by a space).
What is In_array function in PHP?The in_array() function searches an array for a specific value. Note: If the search parameter is a string and the type parameter is set to TRUE, the search is case-sensitive.
|