Array To String PHP | Array To String Conversion in PHP With Example
Summary: In this article, you will learn, Array To String PHP conversion with the example, we will demonstrate the few methods to the array to string conversion in PHP with example. Let’s understand this article in detail.
Array to String PHP Overview
The conversion from a PHP Array to string can simply be done by the PHP implode() function. but however, to convert an Array to String PHP, we can use two different built-in functions, which are available in PHP. These two built-in functions only take one array at a time to convert it into a string.
1. Using json_encode() function to convert an Array to a string
2. Using Serialize function to convert an array to a string
3. Using implode function to convert an array to a string
3.1 Using Indexed Array
3.2 Using Associative array
3.3 Using Multidimensional Array
4. Conclusion:
1. Using json_encode() function to convert an Array to a string
To convert an array to string PHP, one of the common methods to do that is to use the PHP built-in json_encode()
function which is used to returns the JSON representation of a value.
The syntax for using this function as follows-
Basically, this function takes any value as input except resource. But in this program, as given below, we will use an Array as an input value, and furthermore, after that, this function will convert it into a JSON representation of the provided value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $country= array( array( 'Algeria' => 'Algiers' ), array( 'France' => 'Paris' ), array( 'Germany' => 'Berlin' ), array( 'Ireland' => 'Dublin' ) ); echo json_encode($country); ?> |
At first look, it does not seem like a string but this how JSON seems like. If we have a tendency to use var_dump() over json_encode() then it’ll show its data type as a string.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $country= array( array( 'Algeria' => 'Algiers' ), array( 'France' => 'Paris' ), array( 'Germany' => 'Berlin' ), array( 'Ireland' => 'Dublin' ) ); var_dump(json_encode($country)); ?> |
2. Using Serialize function to convert an array to a string
In PHP, The serialize() function is used to returns a string containing a byte-stream representation or illustration of any value that can be stored in PHP. The PHP serialize() function is also used to save all variables in an object.
To serialize any value we want to pass the value to the function and in return, then it’ll return the byte-stream representation or illustration of that value.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
<?php $country= array( array( 'Algeria' => 'Algiers' ), array( 'France' => 'Paris' ), array( 'Germany' => 'Berlin' ), array( 'Ireland' => 'Dublin' ) ); print_r(serialize($country)); ?> |
Let’s break the PHP serialized string.
a:4 -> it says that we have three (3) main elements
i:0/i:1/i:2/i:3 -> it means that says first/second and the third element respectively
a:1 -> its denotes only the number of elements like we saw a:4 (four main elements)
s:5/s:6/s:7 -> its only denotes the length of the string. furthermore, For every data type, there will be different lengths.
3. Using implode function to convert an array to a string
The implode() method is an inbuilt function in PHP that is used to joins array elements and outputs them as a single string. The implode() method is an alias for the PHP join() function and works exactly the same as that of the join() function. it is a very useful method when you want to create one string out of a set of array values.
The syntax for using this function as follows-
Parameter Values
Parameter Value | Description |
---|---|
$glue: | This is the string / special character(s), which is used basically to concatenate array values. By default is an empty string. |
$pieces: | This is the array whose values will stick together using glue. |
This will return all the Array elements concatenated together utilize the glue in the same sequential order in which they seem within the array.
3.1 Using Indexed Array
1 2 3 4 5 6 |
<?php // using indexed array $country = array('Algeria', 'France', 'Germany', 'Ireland'); $country_together = implode(", ", $country); echo $country_together; ?> |
3.2 Using Associative Array
1 2 3 4 5 |
<?php $country = array('Algeria' => 'Algiers', 'France' => 'Paris', 'Germany' => 'Berlin', 'Ireland' => 'Dublin'); $country_together = implode(", ", $country); echo $country_together; ?> |
As described above, all the values from the array will be stick together. Hence, if there’s a circumstance where we want to get the values of the associative array to be glued together then use the same or identical function.
3.3 Using Multidimensional Array
A multidimensional array can be complicated OR simple, which is depending upon the circumstance in the project requirement. We’ll see the basic multidimensional array and for that, we’d like need to write a callback function that will concatenate the values.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
<?php $capital= array( array( 'Algeria' => 'Algiers' ), array( 'France' => 'Paris' ), array( 'Germany' => 'Berlin' ), array( 'Ireland' => 'Dublin' ) ); echo implode(', ', array_map(function ($entry) { return ($entry[key($entry)]); }, $capital)); ?> |
Conclusion:
In this article, you have learned the various ways Array To String PHP conversion with the example. We can use any of the ways to meet or fulfill the requirement. But when we need to decide either to use PHP json_encode() OR serialize() function, I would always suggest that you can use json_encode() as a result of it takes smaller storage space as compared to serialize. I hope you will enjoy it!. if have any query related program then contact me at info@tutorialscan.com I will try to resolve the problem.Stay healthy, stay safe! Happy coding! enjoy it!