what is array in php, types of arrays in php, array function in php, multi dimensional array in PHP, associative array in PHP, indexed array in PHP

How to work with arrays in PHP

An array is a type of data structure that stores or holds multiple values at a single entry that is instead of defining different variables to hold data that is related, array helps in achieving this. For example, if you want to define multiple brands of computers, you can define a variable called computers and then assign all the brands you want in an array.

How to create an array in PHP

We use the array() function to create arrays in PHP

<?php

$arrayname=array();

?>

Types of arrays in PHP

Arrays in PHP are classified into three types

  • Indexed arrays
  • Associative arrays
  • Multidimensional arrays

Indexed arrays are arrays with numerical index where values are stored in a numerical linear form. The index of the first array starts from zero (0)

To create an indexed array, we can follow this format (we are creating an array of different computer brands)

<?php

$computerbrands=array("HP","Samsung","Toshiba");

?>

Or you can assign the index of the array manually

<?php

$computerbrands[0]= "HP";

$computerbrands[1]= "Samsung";

$computerbrands[2]= "Toshiba";

?>

Please note that you cannot print or echo the array as it is, you will receive a notice of array to string conversion error since PHP only outputs strings.

To print or output an array, you have to encode it like shown below

<?php

$computerbrands=array("HP","Samsung","Toshiba");

echo json_encode($computerbrands)

?>

The result will be

["HP","Samsung","Toshiba"]

To get the individual elements of an array, we can get the first by using index 0 and increment to the last value

<?php

$computerbrands=array("HP","Samsung","Toshiba");

// to get the first value in the array

echo $computerbrands[0];

//to get the last value in the defined array, we use index value 2

echo $computerbrands[2];

?>

If you want to output all the values in an indexed array without manually using index values, you can use the for loop to loop through each item in the array.

<?php

$computerbrands=array("HP","Samsung","Toshiba");

$number_of_items_in_array=count($computerbrands);

for($brand=0; $brand<$number_of_items_in_array; $brand++){

echo $computerbrands[$brand];

}

Associative Arrays are arrays that are defined using named keys, they work the same as indexed arrays other than we use names to define and access the value other than indexes

In the above example, we have used to define computer brands, for it to be defined as an associative array, we add name keys to define the brands as shown

<?php

$computerbrands= array("brand_1"=>"HP","brand_2"=>"Samsung","brand_3"=>"Toshiba");

?>

To output a single value from an associative array, we do use the respective name key

<?php

$computerbrands= array("brand_1"=>"HP","brand_2"=>"Samsung","brand_3"=>"Toshiba");

//to output brand 1

echo $computerbrands["brand_1"];

?>

To output all the values in an associative array, we use the for each loop as shown below

<?php

 $computerbrands=array("brand_1"=>"HP","brand_2"=>"Samsung","brand_3"=>"Toshiba");

 foreach($computerbrands as $brand => $brand_value) {

echo $brand ." value is ".$brand_value."<br>";

}

?>

The result will be

brand_1 value is HP

brand_2 value is Samsung

brand_3 value is Toshiba

Multi-dimensional arrays are arrays that contain more than one array, for example, you want to include more than one attribute of the array you have described

In this case that we are using the computer brands array, we may want to add specification for each brand for example the ram and disk space, multi-dimensional arrays allows us to achieve that by defining each brand as a single array

<?php

$computerbrands=array(array("HP","8 gb","500 gb"),array("Samsung","4 gb","320 gb"),array("Toshiba","16 gb","1 TB"));

?>

The output will be a multi-dimensional array

["HP","8 gb","500 gb"],["Samsung","4 gb","320 gb"],["Toshiba","16 gb","1 TB"]

To output the values in a multi-dimensional array, we follow the format of indexed arrays but in this case, we define indexes depending on the array position and the position of an item in that array, for example, Samsung is in the second array, the index value is 1 and first position in that array, the index value is 0.

To output Samsung,

<?php

$computerbrands=array(array("HP","8 gb","500 gb"),array("Samsung","4 gb","320 gb"),array("Toshiba","16 gb","1 TB"));

 echo $computerbrands[1][0];

?>

You can also define a two dimensional array as follows

<?php

$computerbrands = [

    "brand_1" => [

      "name" => "HP",

      "ram" => "4 gb",

      "hard disk" => "500 gb"

    ],

    "brand_2" => [

      "name" => "Samsung",

      "ram" => "2 gb",

      "hard disk" => "230 gb"

    ]

  ];

?>

The output for the defined array is

{"brand_1":{"name":"HP","ram":"4 gb","hard disk":"500 gb"},"brand_2":{"name":"Samsung","ram":"2 gb","hard disk":"230 gb"}}

To print HP brand, you will need to define the brand level first and then access the attribute in the brand

<?php

echo $computerbrands["brand_1"]["name"];

?>

 

We have discussed how to define arrays in PHP, the different types of arrays in PHP, how to work with each array in PHP and finally we have demonstrated examples of working and getting values in each array type. That’s it for this article.