PHP Arrays

An array is a type of data structure that can hold more than one value of the same type. For instance, if you want to store 1000 numbers, you don’t have to set up 1000 of variables. Instead, you can easily set up an array with 1000 elements.

There are three kinds of arrays, and each value in an array is accessed by using an ID called a “array index.”

  • Numeric array: A numeric array is an array whose index is a number. The way values are stored and retrieved is in a straight line.
  • Associative array: An array whose indexes are strings. This stores the values of elements along with their keys instead of in a strict linear index order.
  • Multidimensional array: An array that contains one or more arrays and lets you get to the values using more than one index.

Tips: The function reference PHP Array Functions lists the built-in array functions.

Numeric Array

These arrays can hold numbers, strings, and any other type of object, but their indexes will always be numbers. By default, the index of an array starts at zero.

Example

Here’s an example of how to make a numeric array and get to its elements.

Here, the array() function was used to make an array. function reference tells you more about this function.

<html>
<body>

<?php
/* Here is 1st method to create array. */
$num = array( 1, 2, 3, 4, 5);

foreach( $num as $val ) {
echo “Value is $valu <br />”;
}

/* Here is 2nd method to create array. */
$num[0] = “Sunday”;
$num[1] = “Monday”;
$num[2] = “Thuesday”;
$num[3] = “Wednesday”;
$num[4] = “Thrusday”;

foreach( $num as $val ) {
echo “Value is $val <br />”;
}
?>

</body>
</html>

Output

Value is 1
Value is 2
Value is 3
Value is 4
Value is 5
Value is Sunday
Value is Monday
Value is Thuesday
Value is Wednesday
Value is Thrusday

Associative  Array

Associative arrays and numeric arrays are very similar in how they work, but their indexes are different. The index of an associative array will be a string, so you can make a strong link between the key and the value.

A numerically indexed array would not be the best way to store the salaries of employees. Instead, we could use the names of the employees as the keys in our associative array, and their salaries would be the values.

Tips: Don’t put an associative array inside double quotes when you print it, or it won’t give you any data back.

Example

<html>
<body>

<?php
/* First method to associate create array. */
$age = array(“ram” => 20, “shyam” => 10, “shiv” => 50);

echo “Age of ram is “. $age[‘ram’] . “<br />”;
echo “Age of shyam is “. $age[‘shyam’]. “<br />”;
echo “Age of shiv is “. $age[‘shiv’]. “<br />”;

/* Second method to create array. */
$age[‘ram’] = “50”;
$age[‘shyam’] = “40”;
$age[‘shiv’] = “80”;

echo “Age of Ram is “. $age[‘ram’] . “<br />”;
echo “Age of Shyam is “. $age[‘shyam’]. “<br />”;
echo “Age of Shiv is “. $age[‘shiv’]. “<br />”;
?>

</body>
</html>

Output

Age of Ram is 20
Age of Shyam is 10
Age of Shiv is 50
Age of Ram is 50
Age of Shyam is 40
Age of Shiv is 80

Multidimensional Arrays

In a multi-dimensional array, each element in the main array can also be an array. And each part of the sub-array can also be an array. Multiple index is used to get to the values in a multidimensional array.

Example

In this example, we make a two-dimensional array to store the grades of three students in three subjects.

This is an example of an associative array. In the same way, you can also make a numeric array.

<html>
<body>

<?php
$marks = array(
“ram” => array (
“hindi” => 40,
“english” => 33,
“science” => 50
),

“shyam” => array (
“hindi” => 60,
“english” => 42,
“science” => 39
),

“shiv” => array (
“hindi” => 51,
“english” => 29,
“science” => 60
)
);

/* Accessing multi-dimensional array values */
echo “Marks for ram in hindi : ” ;
echo $marks[‘ram’][‘hindi’] . “<br />”;

echo “Marks for shyam in english : “;
echo $marks[‘shyam’][‘english’] . “<br />”;

echo “Marks for shiv in science : ” ;
echo $marks[‘shiv’][‘science’] . “<br />”;
?>

</body>
</html>

Output

Marks for mohammad in hindi : 40
Marks for qadir in english : 42
Marks for zara in science : 60

People also search

Leave a Comment

Scroll to Top