String Array Functions
So far, we've explored a few functions for mutating strings into new strings. PHP offers a few functions for converting strings into arrays and vice-versa.
The most notable ones are the following:
explode()
: This function takes a string and a delimiter as inputs and outputs an array.implode()
: This is the opposite ofexplode()
. It takes an array and a delimiter and joins all the elements of the array into a single string, inserting the delimiter between each pair of elements.join()
: This is an alias of implode(), and works the same way. You can use either function to join an array into a string.
Remember, you always need to assign the result of these functions to a new variable to use because these functions don't change the original variable. Let's go through each of them.
Array to String
The implode()
function in PHP is used to join or combine elements of an array into a string. You can specify a delimiter to be inserted between each pair of elements.
Using the implode function
- First, define your array. Let's say you have an array of your favorite fruits. In this example, we have an array of fruits.
- Next, you decide on a delimiter, which is a character or string that will be placed between the elements when they are joined into a single string. Let's use a comma and a space for this.
- Now, you call the
implode()
function with your delimiter and your array as parameters. This means "join the elements of $fruits into a string, and put a comma and a space between each one". - The result,
$str
, is now a single string:"apple, banana, cherry"
.
So, the implode()
function is a way of turning a list of things (an array) into a single text string, with a specific character or characters in between each item.
$fruits = array("apple", "banana", "cherry");
$str = implode(", ", $fruits);
echo $str;
apple, banana, cherry
Using the join function
The implode()
function is not the only way to convert arrays into strings. The join()
function is essentially an alias for the implode()
function, meaning it does exactly the same thing. It takes two parameters:
Here's an example of how you might use join()
with our fruits. It results in the same thing.
$fruits = array("apple", "banana", "cherry");
$str = join(", ", $fruits);
echo $str;
apple, banana, cherry
String to Array
The explode()
function in PHP is used to split a string into an array based on a specific delimiter. A delimiter is a character or sequence of characters that marks the boundary between separate elements.
Using the explode function
- Let's say you have a string with a list of your favorite fruits. In this example, the fruits are stored in a variable called
$str
. - You decide that you want to split this string into an array, with each fruit as a separate element. You know that each fruit is separated by a comma(
,
), so you use this as your delimiter. - Now, you call the
explode()
function with your delimiter and your string as parameters. This means "split $str into an array, breaking it up at each comma". - The result,
$fruits
, is now an array.
So, the explode()
function is a way of turning a single text string into a list of items (an array) by specifying the character or characters that separate each item in the original string.
$str = "apple,banana,cherry";
$fruits = explode(",", $str);
print_r($fruits);
Array
(
[0] => apple
[1] => banana
[2] => cherry
)
The Limit Parameter
The explode()
function in PHP has a third optional parameter, which limits the number of array elements returned. If the limit is set, the returned array will contain a maximum of limit elements, and the last element will contain the rest of the string.
Let's continue with our fruits example. If you want to split this string into an array, but you only want the first 3 elements, you can specify a limit.
As you can see, because we've set a limit of 3
, explode()
stops splitting after the first two commas. The rest of the string becomes the third element of the array.
$str = "apple,banana,cherry,date,elderberry";
$fruits = explode(",", $str, 3);
print_r($fruits);
Array
(
[0] => apple
[1] => banana
[2] => cherry,date,elderberry
)
Negative Limit
If you use a negative limit in the explode()
function, it will return all parts of the array except for the last 'n' parts, where 'n' is the absolute value of the limit you specified.
For example, let's use the fruits example again. If you use explode()
with a limit of -2
, like in the example to the right.
The explode()
function has left out the last two parts of the string ("date"
and "elderberry"
) due to the negative limit. This can be useful if you know there are parts at the end of your string that you don't want to include in your array.
$str = "apple,banana,cherry,date,elderberry";
$fruits = explode(",", $str, 3);
print_r($fruits);
Array
(
[0] => apple
[1] => banana
[2] => cherry
)
Key Takeaways
implode()
takes two parameters: a "glue" (delimiter) and an array.- It joins or concatenates all elements of an array into a single string.
- The glue is a string that gets inserted between each element in the resulting string.
explode()
takes at least two parameters: a delimiter and a string. It can optionally take a third parameter to limit the number of elements in the resulting array.- It splits a string into an array, creating a new element every time it encounters the specified delimiter.
- If a limit is specified,
explode()
will return an array with a maximum of that many elements. The last element in the array will contain the rest of the string.