php

Use of implode() Function in PHP

Use of implode() Function in PHP

implode() is a built-in function of PHP that generates a string value by combining the elements of an array with a delimiter. It works like another built-in function of PHP, join(). This function is used for various purposes in the script. For example, when we need to pass the string data with space from one script to another, then the string data can be converted into an array using another built-in function, explode(), before passing the data into another script. The original string data can be retrieved from the array in the second script using the implode() function. How this function can be used in a PHP script is shown in this tutorial.

Syntax:

implode() function can be used in two ways in the script. It can be used with or without a delimiter. The syntaxes of this function are given below:

string implode (array $array) 

When the implode() function is used without any delimiter, it returns a string value by combining all elements of the $array.

string implode (string $glue, array $array) 

When the implode() function is used with any particular delimiter, it returns a string value by adding the delimiter with each element of the $array.

Example 1: Use of implode() function without delimiter

The following script shows the way to use the implode() function without any delimiter to combine the array values. Create a PHP file with the following script.

An array variable named $color is defined with five string values. The implode() function will return a string by combining the array values with empty string that will print later.

//Define an array of strings
$colors = array('White', 'Red', 'Green', 'Blue', 'Pink', 'Purple');
//implode() function without delimiter
$str = implode($colors);
echo "

The output of implode() function without delimiter:

$str
";
?>

Output:

The following output will appear after running the script from the webserver. It shows the six values of the array as a string.

Example 2: Use of implode() function with space delimiter

The following script shows the way to use the implode() function with space(") delimiter. An array of numeric values is defined in the script. The script will return a string by combining the array values with space.

//Define an array of numbers
$numbers = array(12, 56, 23, 89, 65, 90);
//implode() function with space delimiter
$str = implode(", $numbers);
echo "

The output of implode() function with space delimiter:

$str
";
?>

Output:

The following output will appear after running the script from the webserver. The six number values of the array are separated by space in the output.

Example 3: Use of implode() function with comma(,) delimiter

In the previous two examples, no value of the array contains any space. But if the value contains the string of multiple words, then the space delimiter can be used to separate the array values. The following script shows the use of the implode() function with a comma(, ) delimiter. Create a PHP file with the following script.

An array named $names is defined in the script with five string values of multiple words. Next, implode() function is used with comma(,) delimiter to join the values of $names. The returned value of this function is stored in the variable $str that is printed later.

//Define an array of strings with multiple words
$names = array('Nikhil Proctor', 'Kaylee Potter', 'Cloe Whittaker', 'Laila Murphy', 'Rochelle Palmer');
//implode() function with space delimiter
$str = implode(', ', $names);
echo "

The output of implode() function with comma(,) delimiter:

$str
";
?>

Output:

The following output will appear after running the script from the webserver. The values of the array are printed with the comma (,) separator in the output.

Example 4: Use of implode() function in two-dimensional array

A one-dimensional array is used in the previous three examples. The following script shows how the implode() function can be used to join the values of a two-dimensional array. A two-dimensional array named $assoc_arr is declared in the script that contains four values. '
' is used as the delimiter in the script to create a new line after each array value when joining the array values using the implode() function.

//Define an associative array
$assoc_arr = array ('01' => 'google.com', '02' => 'ask.com', '03' => 'bing.com', '04' => 'yahoo.com');
echo "

The list of search engine sites are:

";
//Print the implode() function with
as delimiter
echo "". implode('
', $assoc_arr). "
";
?>

Output:

The following output will appear after running the script from the webserver. The values of the array are printed line by line in the output.

Example 5: Use of implode() function with a word delimiter

In the previous examples, a single character and a
tag are used as a delimiter to join the array values. The following example shows the use of the word as a delimiter in the implode() function. Create a PHP file with the following script.

An array of the six string values is defined in the script. ' and ' is used as delimiter of implode() function in the script. The array values will be printed by adding the word delimiter between the values of the array.

//Define an array of strings
$flowers = array('Rose', 'Lity', 'Tulip', 'Water Lily', 'Orchid', 'Daisy');
//implode() function with 'and ' delimiter
$str = implode(' and ', $flowers);
echo "

The output of implode() function with ' and ' delimiter:

$str
";
?>

Output:

The following output will appear after running the script from the webserver. It shows the array values as a string by adding 'and ' as a separator of the array values.

Conclusion

The use of the implode() function is explained in this tutorial using different types of delimiters. This tutorial will help the readers know the ways of using the implode() function in PHP script and apply this function in their script properly.

Linux için En İyi 5 Ergonomik Bilgisayar Faresi Ürünleri
Uzun süreli bilgisayar kullanımı bileğinizde veya parmaklarınızda ağrıya neden olur mu?? Sert eklemlerden muzdarip misiniz ve sürekli ellerinizi sıkma...
How to Change Mouse and Touchpad Settings Using Xinput in Linux
Most Linux distributions ship with “libinput” library by default to handle input events on a system. It can process input events on both Wayland and X...
Remap your mouse buttons differently for different software with X-Mouse Button Control
Maybe you need a tool that could make your mouse's control change with every application that you use. If this is the case, you can try out an applica...