PHP Array to String Conversion
Arrays are very popular in programming languages, as they are widely used by programmers to perform different tasks. Arrays are part of the first few topics every new developer or programmer learns.
In this example we will learn about arrays, what they are, how to use and manipulate them. For this example we will use:
- A computer with PHP>= 5.5 installed
- notepad++
1. Getting Started
Arrays are a very popular feature of any programming language, because they let you work with large amounts of similar data. In PHP an array is actually an ordered map. A map is a type that associates values to keys.
For instance, let’s say you are writing a script that stores the names of players in a football match, rather than having to create 22 different variables holding the name of each player in a match, you can easily create a single array that holds all the variables. Initializing the array is as simple as looping through it and setting the value of each element in the array.
PHP supports two types of array
- Indexed Arrays: These are arrays where each element is referenced by a numeric index, usually starting from zero. For example the first element in an array has an index of zero.
- Associative Arrays: This type of array is also reffered to as a hash or map. With associative arrays, each element is referenced by a string index.
1.1 Creating Arrays In PHP
The easiest way to create an array in PHP is to use PHP array()
method. It takes a list of values and creates an array containing those values.
index.php
<&;!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title> Array example </title> </head> <body> <?php $names=array("jack","red","white","snow","Jon"); $player = array("name"=>"jack","position"=>"striker","club"=>"MAN U"); echo " $names[0] is a player with $player[club] and is position is $player[position]<br> "; ?> </body> </html>
In line 23 we use the array()
method to create an array which is assigned to variable name. We can access each element in the array through the variable $name
.
In line 25 we create an associative array, where each element is identified by a string index rather than a number.
1.2 Accessing array elements
Once an array has been created, you access the elements inside it, by writing the variable name followed by the index of the element in square brackets. If you want to access the elements of an associative array, simply use string indices rather than numbers.
index2.php
<!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title> Array example </title> </head> <body> <?php $names=array("jack","red","white","snow","Jon"); $player = array("name"=>"jack","position"=>"striker","club"=>"MAN U"); echo " $names[0] is a player with $player[club] and is position is $player[position]<br> "; ?> </body> </html>
1.3 Converting Arrays To String In PHP
PHP provides a function that converts an array to a string. If you want to convert an array to a string you use the implode()
function. This takes two arguements: the string of characters to place between each element in the string and the array containing the elements to place in the string.
index3.php
<!DOCTYPE html> <html lang=eng> <head> <style> html, body{ width:100%; height:100%; margin:0%; font-family:"helvetica","verdana","calibri", "san serif"; overflow:hidden; padding:0%; border:0%; } </style> <meta charset="utf-8" /> <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no, target-densitydpi=device-dpi"/> <title> Array example </title> </head> <body> <?php $names=array("jack","red","white","snow","Jon"); // $player = array("name"=>"jack","position"=>"striker","club"=>"MAN U"); // echo "The names of the players are ". implode($names , ",");//implode converts an array to a string ?> </body> </html>
In the code above, the array names
and a comma are passed to the implode
function.
2. Summary
In this example we learnt about arrays in general, what are they, and why they are so important in programming languages. We also learnt how to create, maniputale and use them in PHP.
3. Download the source code
Download the source code for this tutorial:
You can download the full source code of this example here: phparraytostringconversion