PHP Arrays


In this lesson, you will learn how to create, access and, manipulate arrays. An array is a simple data structure which can hold multiple values in a continuous memory location. Let’s say you have to create a variable to hold a student’s marks. What if you have 100 students in the class? One solution is to create 100 variables to store each student’s data. The ideal solution is to create an array.

Arrays in PHP

Arrays are useful when you have to create a list of variables of the same data type. These can be accessed using their index or key in the array. An array can be considered as an ordered List, which has keys and values. Array values can be other arrays, forming a multi-dimensional array.

Use array() language construct to create an array in PHP. An array takes any number of key, value as an argument.

The syntax

array(
key => value,
key2 => value2,
key3 => value3,
...
keyN => valueN
)

The comma after the last pair is optional and usually omitted to maintain the good programming practice.

$arr = array (1,2,3,4,5);

This statement will create an array of 5 integer values.

In PHP, the key of an array can be an integer or string. The value can be of any type. If floats are used as the key of an array, the fractional part will be truncated. E.g., the key 8.7 will be stored as 8. Boolean values are also cast to integers, too. The true key will be stored as 1 and key false as 0.

The key of an array always starts from zero, specified unless.

If you use the same key for multiple array elements, only the last one will be used as all others are overwritten.

Example I of Arrays in PHP

<?php
$arr = array(
1 => "one",
"1" => "two",
1.5 => "three",
true => "four",
);
var_dump($arr);

?>

Output will be array(1) { [1]=> string(4) “four” }. All the keys in the above code are cast to 1, the value will be overwritten on every new element and the last value “four” is the only left.

If you don’t specify the index, PHP will start an array with zero and will use increment for the next elements.

Example II of Arrays in PHP

<pre>
<?php
$arr = array(1,2,3,4);
var_dump($arr);
?>
</pre>

The output

array(4) {
[0]=>
int(1)
[1]=>
int(2)
[2]=>
int(3)
[3]=>
int(4)
}

You can also access individual array elements using brackets. For example, in the above code $arr[0] will return 1, $arr[1] will return 2 and so on.

Types of an array in PHP

PHP has two types of array:

1- Numeric Arrays

In a numeric array, an index or key is always a number. You can also create an array by storing values at indexes.

Example of Numeric Array in PHP

<?php

$arr[0] = 1;
$arr[1] = 2;
$arr[2] = 3;
$arr[3] = 4;
$arr[4] = 5;

?>

This is a simple array with 5 values. the first value (1) has an index of zero, whereas the last value (5) has an index of 4. The size or length of an array is 5. You can add as many elements you would like to have in an array.

To traverse a numeric array, you can use loops, as explained in Lesson 9, 10, and 11.

<?php

for($i =0; $i<5; $i++)
echo $arr[$i]."<br/> ";

?>

2 – Associative Arrays

This type of array holds strings as key or index of an array.

Example of Associative Array in PHP

<?php

$arr["BSCS001"] = 3.27;
$arr["BSCS002"] = 2.80;
$arr["BSCS003"] = 3.40;
$arr["BSCS004"] = 4.00;
$arr["BSCS005"] = 2.74;

foreach($arr as $key => $value)
echo "Student ID :" .$key . " got ". $value . " CGPA <br/>";
?>

To traverse an associate array use foreach loop, explained in lesson # 11.

Student ID :BSCS001 got 3.27 CGPA
Student ID :BSCS002 got 2.8 CGPA
Student ID :BSCS003 got 3.4 CGPA
Student ID :BSCS004 got 4 CGPA
Student ID :BSCS005 got 2.74 CGPA

Delete an array element

To remove an element, use the unset() function.

Example of Deleting an Array Element

<?php
$arr = array(1,2,3,4,5);

$arr[] = 6;

echo "<pre>";
print_r($arr);
echo "</pre>";

unset($arr[4]);

echo "Array after deleting 4th element";
echo "<pre>";
print_r($arr);
echo "</pre>";

?>

 

The output

Array
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[4] => 5
[5] => 6
)
Array after deleting 4th element
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
[5] => 6
)

PHP foreach Loop Tutorial Home PHP Get and Post methods
Tutorials Panel

About Tutorials Panel

programming tutorial and source code for Software Engineers and developers. https://twitter.com/TutorialsPanel

View all posts by Tutorials Panel →