Array in php – Indexed, Associative And Multidimensional

PHP Array- Indexed, Associative And Multidimensional Array

Type of array 

  1. Indexed
  2. Associative
  3. Multidimensional

You may like

1. PHP Indexed Arrays

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";

The following example creates an indexed array named $cars, assigns three elements to it, and then prints a text containing the array values:

 

Example 1.

$cars = array("Volvo", "BMW", "Toyota");

echo "I like " . $cars[0] . ", " . $cars[1] . " and " . $cars[2] . ".";

 

Output:

I like Volvo, BMW and Toyota.

These type of arrays can be used to store any type of elements, but an index is always a number. By default, the index starts at zero. These arrays can be created in two different ways as shown in the following example:

 

Example 2.

// One way to create an indexed array

$name_one = array("Zack", "Anthony", "Ram", "Salim", "Raghav");

// Accessing the elements directly

echo "Accessing the 1st array elements directly:n";

echo $name_one[2], "n";

echo $name_one[0], "n";

echo $name_one[4], "n";

// Second way to create an indexed array

$name_two[0] = "ZACK";

$name_two[1] = "ANTHONY";

$name_two[2] = "RAM";

$name_two[3] = "SALIM";

$name_two[4] = "RAGHAV";

// Accessing the elements directly

echo "Accessing the 2nd array elements directly:n";

echo $name_two[2], "n";

echo $name_two[0], "n";

echo $name_two[4], "n";

 

Output:

Accessing the 1st array elements directly:

Ram

Zack

Raghav

Accessing the 2nd array elements directly:

RAM

ZACK

RAGHAV

 

2. PHP Associative Arrays

The associative arrays are very similar to numeric arrays in term of functionality but they are different in terms of their index. Associative array will have their index as string so that you can establish a strong association between key and values.

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

NOTE − Don’t keep associative array inside double quote while printing otherwise it would not return any value.

Example 1.

/* First method to associate create array. */

         $salaries = array("mohammad" => 2000, "qadir" => 1000, "zara" => 500);

echo

"Salary of mohammad is ". $salaries['mohammad'] . "<br />";

         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";

         echo "Salary of zara is ".  $salaries['zara']. "<br />";

/* Second method to create array. */

         $salaries['mohammad'] = "high";

         $salaries['qadir'] = "medium";

         $salaries['zara'] = "low";

echo

"Salary of mohammad is ". $salaries['mohammad'] . "<br />";

         echo "Salary of qadir is ".  $salaries['qadir']. "<br />";

         echo "Salary of zara is ".  $salaries['zara']. "<br />";

 

Output:

Salary of mohammad is 2000

Salary of qadir is 1000

Salary of zara is 500

Salary of mohammad is high

Salary of qadir is medium

Salary of zara is low

 

Example 2.

Associative arrays are arrays that use named keys that you assign to them.

There are two ways to create an associative array:

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

or:

$age['Peter'] = "35";

$age['Ben'] = "37";

$age['Joe'] = "43";

The named keys can then be used in a script:

 

Example 2.

$age = array("Peter"=>"35", "Ben"=>"37", "Joe"=>"43");

echo "Peter is " . $age['Peter'] . " years old.";

Output:

Peter is 35 years old.

Loop Through an Associative Array

To loop through and print all the values of an associative array, you could use a foreach loop, like this:

 

Example

$age = array("vikram"=>"50", "kul"=>"30", "deep"=>"70");

foreach($age as $x => $x_value) {

  echo "Key=" . $x . ", Value=" . $x_value;

  echo "<br>";

}

 

Output:

Key=vikram, Value=50

Key=kul, Value=30

Key=deep, Value=70

 

Test Yourself  Exercises: 

Create an associative array containing the age of vikram, kul and deep.

$age = array("vikram""50", "kul""30", "deep""70");

please comment me answer box

 

Complete PHP Array Reference

For a complete reference of all array functions, go to our complete PHP Array Reference.

The reference contains a brief description, and examples of use, for each function!

Related:

You may like

Tags:

Add a Comment

Your email address will not be published.