Domesticated Brain

PHP Arrays

Before we go through the PHP Arrays, it’s better to get an idea about the arrays in programming

An array is a way to store a fixed number of values of a single defined data type inside one variable. When you take it as normal, it’s a series of objects. Each object in an array is called an array element and can access each element by its numerical index.

In PHP, there are three types of arrays to be taken under consideration

  • PHP Indexed array
  • PHP Associative array
  • PHP Multidimensional array

 

PHP Indexed arrays – Index arrays are arrays with a numeric index

In PHP programming language there are two ways of defining index arrays.

We can let program to assign index automatically or we can defined it in our own way

Method of assigning index automatically

We have four elements in our array named $devices. The four elements are computer, laptops, tabs and Mobile phones

<?php

$devices = array ("Computers", "Laptops", "Tabs", "Mobile phones");

   print_r($devices);

?>

 

Out put of the above PHP program -

Array ( [0] => Computers [1] => Laptops [2] => Tabs [3] => Mobile phones )

 

Note that an index always starts at 0. Key 0 is holding a value of computer 

Key 0 = value of computer

Key 1 = value of Laptops

Key 2 = value of Tabs

Key 3 = value of Mobile phones

 

Method of assigning index manually

$devices [0] = " Computers ";

$devices [1] = " Laptops ";

$devices [2] = " Tabs ";

$devices [3] = " Mobile phones ";

<?php
$devices = array ("Computers", "Laptops", "Tabs", "Mobile phones");

$devices [4] = " Test device";

print_r($devices);

?>

 

Out put of the above PHP program -

Array ( [0] => Computers [1] => Laptops [2] => Tabs [3] => Mobile phones [4] => Test device ) 

 

We use count function to count number of elements in an array

<?php

$devices = array ("Computers", "Laptops", "Tabs", "Mobile phones");

   echo count ($devices);

?>

 

Out put of the above PHP program -

4

 

PHP Associative Arrays

PHPP Associative arrays use named keys assigned by the programmer instead of using index assigned by automatically. That means it allows us to change the key of each element

As an example we are going to assign an average value for each device to store more data in our array. To do this we use elements in our previous examples as keys for values of average prices for each device

  • Computers = 5000
  • Laptops = 4000
  • Tabs = 3000
  • Mobile phones = 2000

 

<?php

$devices = array ("Computers"=> 5000, "Laptops"=>4000, "Tabs"=>3000, "Mobile phones"=>2000);

   print_r($devices);

?>

 

Out put of the above PHP program -

Array ( [Computers] => 5000 [Laptops] => 4000 [Tabs] => 3000 [Mobile phones] => 2000 )

 

PHP Multidimensional arrays – Multidimensional arrays consist of one or more arrays

Let’s find out How to declare and how to access each element of an array

As an example we are going to categories cars into two categories which named as Japanese and European. We can store following information within a PHP associative array as shown below

<?php

$cars = array ("Japanese"=>array('Toyota','Mitsubishi') ,

              "European"=>array('BMW','Benz'));

echo $cars['Japanese'][0];

?>

 

Out put of the above PHP program -

Toyota

 

Let’s find out how we can access each element in a PHP multidimensional array

To access each element use the following piece of code 

echo $cars['Japanese'][0];

 

For more details, Please refer the PHP manual and the W3Schools web site.

If you have found  what you expected please comment and like on our facebook page