PHP - Arrays

  »   PHP Introduction  »   PHP Tutorial - Arrays

An array is a type of data structure that allows you to store multiple values inside of a variable, on a key-value basis. Here is a simple example of an array in PHP.

php$product = array("milk", "bread", "potato");
echo "Please buy " . $product[0] . ", " . $product[1] . " and " . $product[2] . " from the store.";

This is a good substitute for storing every product in its own variable. Imagine you had a few hundreds of products and you have to list them all:

php$product_01 = "product_01";
$product_02 = "product_02";
$product_03 = "product_03";
$product_99 = "product_99";

This is not a good way to do it. Here is how you achieve the same thing using an array. An array in PHP allows you to access any value by referring it by his index or key.

Array types and how to create an array in PHP

To better understand arrays, imagine them as containers that you can use to store data. PHP has three different types of arrays or containers.

  • indexed arrays - this type of array is using numbers for their indexes or keys.
  • associative arrays - this type of arrays is using strings for their indexes or keys.
  • multidimensional arrays - arrays that contain various other arrays on one or more of their key positions

To create a simple array with no data in PHP, you can call the built in array() function.

php$myArray = array();

Now that you created an array container, let's go and put some values inside it.

Create PHP indexed array

Indexed arrays are the most simple type of array.

php$products = array("product_1", "product_2", "product_3");

On the other hand, you can manually assign the index that an array will use to store a specific value.

php$products[0] = "product_1";
$products[1] = "product_2";
$products[2] = "product_3";

Please bear in mind that if no index number is specified then the index values always start from 0.

The first and the second method of creating an array are both equivalents. But let's complicate a little bit and imagine that for some reason you want a specific index for a specific value. Like for example, you want to assign your product name on the product ID value and that your product IDs are not consecutive. Here is how you can accomplish that

php$products_array = array(22 => "product_1", 24 => "product_2", 102 => "product_3");

// this is equivalent
$products_array[22] = "better_product";
$products_array[24] = "cool_product";
$products_array[102] = "awesome_product";

Create PHP associative array

PHP associative arrays are created just the same way as indexed arrays. Now imagine you want to create a container or an array that contains all product details. Here is an example to illustrate that:

php$products = array(
	"product_ref" => "15456645",
	"product_name" => "Awesome White Milk",
	"product_color" => "white"

// this is also equivalent
$products_array["product_ref"] = "15456645";
$products_array["product_name"] = "Awesome White Milk";
$products_array["product_color"] = "white";

Create PHP multidimensional arrays

Now think about you want to create an array that contains all products and all the product details. To do that you will need a multidimensional array or an array of arrays. You can have as many arrays inside arrays as you like.

php$products = array(
	array( product_1_data ),
	array( product_2_data ),
	array( product_3_data )

This is a very simplified version of a multidimensional array. We will learn more about PHP multidimensional arrays in a future tutorial.