Wednesday, November 16, 2011

How to Create Arrays in PHP

arrays in php

Summary: In this tutorial I will explain you what is array, where they are used, how to create arrays in php, difference between indexed arrays and associative arrays, how to access values from arrays, how to create multi dimensional arrays and lastly there is a video tutorial on Arrays in PHP in both English and Hindi.

Arrays

Arrays are used to store multiple values of multiple data types in PHP. In other programming languages you may have heard that array is collection of multiple values of similar data type but in PHP you can store strings, integers, floats and booleans all inside a single array.

For example you want to store names of ten students, using string variables you will have to create ten variables to store them which will be a difficult task, but using arrays you can store all of them inside a single variable. Lets see how you will do this in PHP :

<?php

 // Create array to store multiple values
 $names = array(
 "ankit",
 "ashish",
 "aman",
 "ankur",
 "arpita"
 );
    
?>

You will use the keyword array and then and opening and closing parenthesis to create an array in PHP. Then place the values inside arrays using commas, note that last elements should not contain comma otherwise it will generate error.

You can also insert, update and clear values from already existing arrays:

<?php

 // Create array to store multiple values
 $names = array(
 "ankit",
 "ashish",
 "aman",
 "ankur",
 "arpita"
 );

 // Inserting values to already exisiting array, value get added to last 
 $names[] = "Amit";

 // Updating Arrays
 $names[1] = "Sumit";

 // Clearing all values in an Array, also know as making empty array
 $names[] = array();
    
?>

Indexed Array

The type of array we created just now is called indexed array, because to access the array values we make use of Index position of array values. Index position always starts from zero. Lets see how you will access the indexed array and echo its values.

<?php

 // Create array to store multiple values
 $names = array(
 "ankit",
 "ashish",
 "aman",
 "ankur",
 "arpita"
 );
    
 // value ankit is at index 0
 echo $names[0];
 
 // value ashish is at index 1
 echo $names[1];

 // if you write out of index value, it will generate error, index 7 is not in array $names
 echo $names[7];

?>

You can place values of any data type inside arrays, there is not any limitation that you should always put values of similar data type in a array. So this array is completely legal in PHP:

<?php

 $myArray = array(
 "ankit",
 25,
 5.7,
 true
 );

 echo "<h3>My Details</h3>";
 echo "<p> Name: " . $myArray[0] . "</p>";
 echo "<p> Age: " . $myArray[1]. "</p>";
 echo "<p> Height: " . $myArray[2] . " </p>";
    
?>

Associative Array

Beside creating indexed array, you can also create associative arrays in PHP. In associative arrays there are keys and values. Rather than accessing values using index position you will use Key name to access each value. Lets see the example of associative array:

<?php
 
 // Associative Arrays in PHP
 $myArray = array (
 
 "name" => "Ankit Agrawal",
 "age" => 25,
 "height" => 5.7

 );
    
?>

In associative array you define the keys within quotes and its values using equal symbol followed by greater than symbol =>

Lets see how to access values from associative arrays:

<?php
 
 //  Creating Associative Arrays in PHP
 $myArray = array (
 
 "name" => "Ankit Agrawal",
 "age" => 25,
 "height" => 5.7

 );

 //  Accessing  Associative Arrays in PHP
 echo "<h3>My Details</h3>";
 echo "<p> Name: " . $myArray["name"] . "</p>";
 echo "<p> Age: " . $myArray["age"]. "</p>";
 echo "<p> Height: " . $myArray["height"] . " </p>";
    
?>

Multi Dimensional Arrays

You can also place arrays inside an array in PHP, which is know as Multi Dimensional arrays, the concept is very simple you just need to understand the basics, that is, in PHP an array can store Multiple values of Multiple data types, and array is itself is a data type.

<?php
 
 //  Creating Multi Dimensional  Arrays in PHP

 $studentsArray = array (
 
  // Array named student1, remember to add commas if you are adding more than one arrays
        "student1" => array (
                "name" => "Ankit Agrawal",
                "age" => 25,
                "height" => 5.7
        ),
         
  // Array named student2 
        "student2" => array (
                "name" => "Ashish Agrawal",
                "age" => 27,
                "height" => 5.8
        ),

  // Array named student3 
        "student3" => array (
                "name" => "Arpita Agrawal",
                "age" => 24,
                "height" => 5.5
        )
 
 );

 //  Accessing  Student1 Details
 echo "<h3>Details of Student 1</h3>";
 echo "<p> Name: " . $studentsArray["student1"]["name"] . "</p>";
 echo "<p> Age: " . $studentsArray["student1"]["age"]. "</p>";
 echo "<p> Height: " . $studentsArray["student1"]["height"] . " </p>";
  
 //  Accessing  Student2 Details
 echo "<h3>Details of Student 1</h3>";
 echo "<p> Name: " . $studentsArray["student2"]["name"] . "</p>";
 echo "<p> Age: " . $studentsArray["student2"]["age"]. "</p>";
 echo "<p> Height: " . $studentsArray["student2"]["height"] . " </p>";
    
 //  Accessing  Student3 Details
 echo "<h3>Details of Student 1</h3>";
 echo "<p> Name: " . $studentsArray["student3"]["name"] . "</p>";
 echo "<p> Age: " . $studentsArray["student3"]["age"]. "</p>";
 echo "<p> Height: " . $studentsArray["student3"]["height"] . " </p>";

?>

The code above may look complex, but its actually very simple, we have placed three arrays student1, student2 and student2 inside $studentsArray and we are accessing it using arrayName[inisdeArrayName][key].

Arrays in PHP [Video tutorial in English]


Arrays in PHP [Video tutorial in Hindi]

This is the seventh chapter in which I explained you Arrays in PHP, in the next chapter I will explain you Conditional Statements for making decisions in PHP.