Contact Information

KodeBlog

We're Available 24/ 7. Drop us an email.

PHP - Arrays

In the realm of PHP web development language, an Array is a structure of data that stores either one or more than one type of values as a single value. For example if you want to store 50 numbers instead of defining 50 different variables for each number then it gets easier to define with the help of array with the length of 50.

PHP comes with three different types of arrays and each of array value is accessed using an ID which is called array index. These three types of arrays are:

Number Array - Associative Array - Multidimensional Array

Topics to be covered

We will cover the following topics in this lesson

  • Numeric Array
  • Associative Array
  • Multidimensional Array
  • Summary

Numeric Array

Numeric Arrays are used to store numerical values, strings and any object with the index that is represented by numbers. By default array index is started from zero.

For example

<?php

/* one of the method to creat numeric array */
$varnum = array( 1, 2, 3, 4, 5);

foreach( $varnum as $value ) {
	echo "Value is $value <br />";
}

/* another method for using numeric array */
$varnum[0] = "1st";
$varnum[1] = "2nd";
$varnum[2] = "3rd";
$varnum[3] = "4th";
$varnum[4] = "5th";

foreach( $varnum as $value ) {
	echo "The Value for this particular Array is $value <br />";
}

/* another method for creating arrays )*/

$varnum = [1, 2, 3, 4, 5];

foreach( $varnum as $value ) {
	echo "The Value for this particular Array is $value <br />";
}
?>

 

Above code will return the following output

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
The Value for this particular Array is 1st
The Value for this particular Array is 2nd
The Value for this particular Array is 3rd
The Value for this particular Array is 4th
The Value for this particular Array is 5th
The Value for this particular Array is 1
The Value for this particular Array is 2
The Value for this particular Array is 3
The Value for this particular Array is 4
The Value for this particular Array is 5

In the above example we use numeric arrays in two different ways each one stored values starting from array slot 0 and so on. That is to say array[0] is basically value 1st and array[1] is value 2nd. That's the way it is and this is how array works whether whichever type of array we use.

Associative Array

Associative arrays are no different than numeric values in terms of functionality but they are different when it comes to indexing, as they use different type of indexing compared to numeric arrays. In order to establish a strong association between values and key associative array will be having their index as string.

For example if you want to marks for students after exams, a numerically indexed array would not be eligible choice. Instead of using numeric arrays we will use associative array so that the student names as the key and marks as the value can be used respectively.

For example

 <?php

/* one of the method to create associate array */
$marks = array("Fred" => 443, "Alice" => 693, "Joe" => 511);

echo "Marks of Fred is ". $marks['Fred'] . "<br />";
echo "Marks of Alice is ".  $marks['Alice']. "<br />";
echo "Marks of Joe is ".  $marks['Joe']. "<br />";

/* And this is another method to create associative array */
$grade['Fred'] = "B";
$grade['Alice'] = "A+";
$grade['Joe'] = "A";
echo "Grade of Fred is ". $grade['Fred'] . "<br />";
echo "Grade of Alice is ".  $grade['Alice']. "<br />";
echo "Grade of Joe is ".  $grade['Joe']. "<br />";

/* one of the method to create associate array */
$marks = [
    "Fred" => 443, 
    "Alice" => 693, 
    "Joe" => 511
];

echo "Marks of Fred is ". $marks['Fred'] . "<br />";
echo "Marks of Alice is ".  $marks['Alice']. "<br />";
echo "Marks of Joe is ".  $marks['Joe']. "<br />";

?>

 

The above code will return the following output

Marks of Fred is 443
Marks of Alice is 693
Marks of Joe is 511
Grade of Fred is B
Grade of Alice is A+
Grade of Joe is A
Marks of Fred is 443
Marks of Alice is 693
Marks of Joe is 511

Multidimensional Array

Multidimensional Arrays more like a sci-fi concept about some multi-dimensional movie, gives you the ability to assign each element within the array to be array themselves and each element of the sub-array can also be arrays and even so on and on (much like a loop of arrays for a specified amount/quantity set by user).

Moreover values of the multi-dimensional arrays are accessed by using multiple indexes (on indices).

<?php

$marks = array( 
    "Fred" => array (
        "physics" => 35,
        "maths" => 30,	
        "history" => 39,
        "geography" => 44
    ),
    "Alice" => array (
        "physics" => 30,
        "maths" => 32,
        "history" => 29,
        "geography" => 14
    ),
    "Joe" => array (
        "physics" => 31,
        "maths" => 22,
        "history" => 39,
        "geography" => 68
    )
);

/* Accessing multi-dimensional array values */
echo "Marks for Fred in physics : " ;
echo $marks['Fred']['physics'] . "<br />"; 

echo "Marks for Alice in maths : ";

echo $marks['Alice']['maths'] . "<br />"; 

echo "Marks for Joe in history : " ;
echo $marks['Joe']['history'] . "<br />"; 

?>

Above code will return the following output

Marks for Fred in physics : 35
Marks for Alice in maths : 32
Marks for Joe in history : 39

Summary

Arrays are used to store multiple values in the same variable name. The values stored in the array are then accessed via index numbers or key name as in the case of associative arrays. Index arrays use index numbers to store values. The index number starts from 0 not 1. Associative arrays use a pair of key and value to store values. Multiple arrays contain other arrays internally.

SUPPORT US

If you found this tutorial useful, support us by using the social media buttons to like and share the tutorial. If you didn’t find it useful, please use the comments section below to let us know how we can do better next time.

Subscribe to our newsletter, like our Facebook fan page or follow us on Twitter to get free updates when we publish new tutorials

Author: Rodrick Kazembe

Rodrick is a developer who works on Desktop, Web and Mobile Applications. He is familiar with Python, Java, JavaScript, C++, C#, Kotlin, PHP, Python and the list goes on. Rodrick enjoys sharing knowledge especially when it comes to technology.

PHP - Strings

PHP 7 Return Type Declarations

Leave A Reply