PHP’s implode() function takes array as a value and returns a string.
Implode function takes 2 parameters seprator
and $array
let’s find out how it works?
implode(separator,array)
Parameter | Detail |
---|---|
Separator | It’s an optional parameter. This parameter allows you to insert seprator type in your array element. It comes with ” “ empty string. |
Array | This is an array which will be converted to a string. It’s an enviable parameter. |
Let’s find out how array to string conversion works in PHP, In the below examples i am going to take an array and convert it to the string using PHP’s implode()
function.
<?php
$array = ['Waiting', 'for', 'Avenger', 'End', 'Game'];
echo implode(" ",$array); // Result: Waiting for Avenger End Game
?>
In below example, we are going to skip the separator parameter. You will se that implode function will add an empty string between array’s elements.
<?php
$array = ['A', 'v', 'e', 'n', 'g', 'e', 'r'];
echo implode($array); // Avenger
?>
Let’s check out if your consist non-string values, in this condition PHP’s implode function will convert all your non-string values to string values.
Note: Implode function convertes true to 1 and NULL & empty value to empty string.
<?php
$array = [1, false, 0, true, NULL, 0.07];
echo implode(', ', $array); // 1, , 0, 1, , 0.07
?>
Conversion result for the some non-string values by implode function:
If you have multiple arrays within your arrays in PHP it will return arrays as a result. Check out in the example below.
<?php
$movies = [
'comedy' => ['In the Loop', '21 Jump Street', 'Elf ', 'This Is the End'],
'war' => ['Dunkirk', 'Hacksaw Ridge', 'Inglourious Basterds', 'Defiance'],
'action' => ['La La Land', 'The Notebook', 'About Time', 'Twilight'],
'sci fi' => ['Interstellar', 'Annihilation', 'Gravity', 'Inception'],
];
echo implode(', ', $movies); // Array, Array, Array, Array
?>
Now lets convert multilevel array into string in PHP using custom function below with Implode() function.
<?php
// $movies array
$movies = [
'comedy' => ['In the Loop', '21 Jump Street', 'Elf ', 'This Is the End'],
'war' => ['Dunkirk', 'Hacksaw Ridge', 'Inglourious Basterds', 'Defiance'],
'action' => ['La La Land', 'The Notebook', 'About Time', 'Twilight'],
'sci fi' => ['Interstellar', 'Annihilation', 'Gravity', 'Inception'],
];
// function to convert arrays to string
function convertArraysToString($array, $separator = ', ') {
$str = '';
foreach ($array as $Array) {
$string .= implode($separator, $Array);
}
return $string;
}
// pass $movies array and call the function
echo convertArraysToString($movies);
// Result => In the Loop, 21 Jump Street, Elf , This Is the EndDunkirk, Hacksaw Ridge, Inglourious Basterds, DefianceLa La Land, The Notebook, About Time, TwilightInterstellar, Annihilation, Gravity, Inception
?>
In this part of this tutorial, we are going to explore more techniques for converting PHP arrays into strings. We are going to work with json_encode()
and serialize()
methods.
PHP offers json_encode() function to handle JSON, it converts objects, arrays into JSON.
<?php
$array = ["Tony Stark", "Steve Rogers", "Bruce Banner", "Thor"];
$myJson = json_encode($array);
echo $myJson; // ["Tony Stark","Steve Rogers","Bruce Banner","Thor"]
?>
The serialize() function in PHP produces a storable representation of a value
<?php
$array = ["Tony Stark", "Steve Rogers", "Bruce Banner", "Thor"];
$myJson = serialize($array);
echo $myJson;
//Result => a:4:{i:0;s:10:"Tony Stark";i:1;s:12:"Steve Rogers";i:2;s:12:"Bruce Banner";i:3;s:4:"Thor";}
?>
If you want to learn how to get the current route's name, component pathname or…
React show and hide example. In this tutorial, we will show you how to step…
Tabs are one of the best UI elements for displaying multiple contents in a single…
In this tutorial, we will learn how to create a toast notification component in the…
Bootstrap offers tons of custom UI solutions. On top of that, it is easy to…
React js counter using the useReducer hook example. In this post, we will learn how…