How to Convert PHP Array To String With Example
Summary: In this article, you will learn how to convert PHP array to string with example. furthermore, also learn the various ways that you can convert PHP array to string in PHP. let’s understand this article in details.
To Convert PHP Array To String Overview:
This tutorial covers a two ways regarding to convert array to string:
1. Using implode( ) function
2. Using explode( ) function
3. Using json_encode( ) function
1. Using implode( ) function
By using the PHP implode() function, we can convert all array elements into a string. This function returns the string. The separator parameter in implode() function is optional.
Syntax:
As you can see in the above example, at the first line, an array variable is declared and assigned some values.
Furthermore, in the next line, the PHP implode() function will convert the array into a string. there are two parameters passed in the implode() function. 1st is the separator and the other one is array_name.
The implode function in PHP returns a string consisting of array element values joined using a string that you just specify:
Here, the string used to separate the array values is the 1st argument. If you leave out that argument and just simply pass the array you want to join, the string between array elements defaults to an empty string:
Furthermore, suppose that if arrays consist of values that aren’t strings, thus those values will be converted to strings for inclusion in the string returned by the PHP implode function:
2. Using explode() function
By using PHP explode() function, you can convert a string into array elements. here, you can pass three arguments in it. The 1st one is for separator, the second for array_name, and the last one for the limit.
Syntax:
Therefore, as you can see in the above example, a string variable is assigned a few values. Then, PHP explode() function breaks this string into an array. furthermore, we used print_r() function which prints all the array elements and their indexes.
3. Using json() Function
In PHP, the objects can be converted into JSON String by using the json_encode() function. furthermore, common use of JSON is to read data from a web server and display the data on a web page.
Syntax:
name="Aditya"; @$myObject->age=30; @$myObject->city="Lucknow"; @$myObject->country="India"; //json_encode() put data into associative array with index $myJSON=json_encode($myObject); echo($myJSON); ?>
therefore, as you can see 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.
{"name":"Aditya","age":30,"city":"Lucknow","country":"India"}PHP offers a lot of ways in which to convert arrays and other values to strings: json_encode and serialize. 1st, we demonstrate how to applying json_encode to the following array:
'Pinki',
'age' => 30,
'husband' => 'aditya',
'status' => null,
'friends' => ['John', 'arti', 'dyana']
];
echo json_encode($student);
?>
{"name":"Pinki","age":30,"husband":"aditya","status":null,"friends":["John","arti","dyana"]}Furthermore, next we pass the same or identical array to the serialize function:
'Pinki',
'age' => 30,
'husband' => 'aditya',
'status' => null,
'friends' => ['John', 'arti', 'dyana']
];
echo serialize($student);
?>
a:5:{s:4:"name";s:5:"Pinki";s:3:"age";i:30;s:7:"husband";s:6:"aditya";s:6:"status";N;s:7:"friends";a:3:{i:0;s:4:"John";i:1;s:4:"arti";i:2;s:5:"dyana";}}PHP Array to String Question and Answer
Question: How to Convert Array of Arrays to String in PHP
Answer: Suppose that if the array we pass to the implode function contains elements that are arrays, then Array will be the output for each sub-array:
['Arrow', 'Peter England', 'Van Heusen', 'Louis Phillipe'],
'shoes' => ['Nike', 'adidas', 'Bata'],
'watch' => ['Piaget', 'Harry Winston', 'Benson']
];
echo implode(', ', $brands);
// Array, Array, Array
?>
Also, an error will be logged or displayed: Notice: Array to string conversion.
An array consisting of a single level of sub-arrays, like the example above, could be converted to a string using the following function:
['Arrow', 'Peter England', 'Van Heusen', 'Louis Phillipe'],
'shoes' => ['Nike', 'adidas', 'Bata'],
'watch' => ['Piaget', 'Harry Winston', 'Benson']
];
function subArraysToString($myarr, $sep = ', ') {
$mystr = '';
foreach ($myarr as $val) {
$mystr .= implode($sep, $val);
$mystr .= $sep; // add separator between sub-arrays
}
$mystr = rtrim($mystr, $sep); // remove last separator
return $mystr;
}
// $brands array from example above
echo subArraysToString($brands);
?>
Conclusion:
In this article, you have learned various ways to convert PHP array to string with example. 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!