PHP Arrays

PHP Arrays

Hope this article find you well.

Today we will learn about the PHP Arrays.

So read the article till the end and i will make sure you know everything about PHP Arrays.

Before i proceed, i would request you to do SUBSCRIBE to ShoutMe360.com so that you can get regular updates.

So lets start…..

What is an Array?

An array is a special variable, which can hold more than one value at a time of single type. For example if you want to store 100 numbers then instead of defining 100 variables its easy to define an array of 100 length.

Example:
<?php
$cars = array(“Volvo”, “BMW”, “Toyota”);
echo “I like ” . $cars[0] . “, ” . $cars[1] . ” and ” . $cars[2] . “.”;
?>

An array can hold multiple values under a single name, and you can access the values by referring to an index number.

Create an Array in PHP

In PHP, the array() function is used to create an array:

array();

In PHP, there are three types of arrays:

Indexed/Numeric array – An array with a numeric index. Values are stored and accessed via numeric index.

Associative array – An array with strings as index.

Multidimensional array – An array containing one or more arrays.

PHP Indexed/Numeric Arrays

These arrays can store numbers, strings and any object but their index will be accessed by numbers. By default array index starts from zero.

There are two ways to create indexed arrays:

The index can be assigned automatically (index always starts at 0), like this:

$cars = array(“Volvo”, “BMW”, “Toyota”);

or the index can be assigned manually:

$cars[0] = “Volvo”;
$cars[1] = “BMW”;
$cars[2] = “Toyota”;

<?php
for($x = 0; $x < $arrlength; $x++) {
echo $cars[$x];
echo “<br>”;
}
?>

This will produce the following result-
Volvo
BMW
Toyota

PHP Associative Arrays

Associative arrays are arrays that use named keys that you assign to them. Associative array have their index as string so that you can establish a very strong association between key and values.

There are two ways to create an associative array:

$age = array(“Peter”=>”35000”, “Ben”=>”37000”, “Joe”=>”43000”);

or:

$age[‘Peter’] = “35000”;
$age[‘Ben’] = “37000”;
$age[‘Joe’] = “43000”;

To store the salaries of employees in an array, a numerically indexed array is good but not the best choice. Instead, we can use the employees names as the keys in our associative array, and the value would be their respective salary.

We can access above declared array as below:

<?php
echo “Salary of Peter is “. $salaries[‘Peter’] . “<br />”;
echo “Salary of Ben is “. $salaries[‘Ben’]. “<br />”;
echo “Salary of Joe is “. $salaries[‘Joe’]. “<br />”;
?>

This will produce the following result ?

Salary of Peter is 35000
Salary of Ben is 37000
Salary of Joe is 43000

PHP Multidimensional Arrays

A multidimensional array is an array containing one or more arrays.

PHP understands multidimensional arrays that are two, three, four, five, or more levels deep. However, arrays more than three levels deep are hard to manage for most people.

In this example we create a two dimensional array to store marks of three students in three subjects ?

<?php
$marks = array(
“Peter” => array (
“physics” => 35,
“maths” => 30,
“chemistry” => 39
),

“Ben” => array (
“physics” => 30,
“maths” => 32,
“chemistry” => 29
),

“Joe” => array (
“physics” => 31,
“maths” => 22,
“chemistry” => 39
)
);

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

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

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

This example is an associative array, you can create numeric array in the same fashion.

Hope this explains you enough about the PHP Arrays.

This is Lalit signing off for today.

If you like this post please give a thumbs up and keep it sharing.

Hey don’t forget to SUBSCRIBE please so that you can get notification on new posts.

Tada

Leave a Reply