Learn PHP Programming

A backend server scripting language, and a powerful tool for making dynamic and interactive Web pages. Learn each topic with examples and output.

PHP Introduction
Control Structures
Php Forms
Php Advanced

Php Array

An array stores multiple values in one single variable:

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

What is an Array?

An array is a special variable, which can hold more than one value at a time.

Create an Array in PHP

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

In PHP, there are three types of arrays:

Get The Length of an Array - The count() Function

The count() function is used to return the length (the number of elements) of an array:

<?php
$cars = array("Volvo", "BMW", "Toyota");
echo count($cars);
?>
3

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:

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

Loop Through an Indexed Array

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

<?php
$cars = array("Volvo", "BMW", "Toyota");
$arrlength = count($cars);

for($x = 0; $x < $arrlength; $x++) {
  echo $cars[$x];
  echo "
"; } ?>
Volvo
BMW
Toyota

Associative Array

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

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

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

foreach($age as $x => $x_value) {
  echo "Key=" . $x . ", Value=" . $x_value;
  echo "<br>";
}
?>
Key=Peter, Value=35
Key=Ben, Value=37
Key=Joe, Value=43

PHP - Multidimensional Arrays

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

PHP supports 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.

The dimension of an array indicates the number of indices you need to select an element.

For a two-dimensional array you need two indices to select an element

For a three-dimensional array you need three indices to select an element

Sno Name Profession
1 Aryan Game developer
2 Rankush Full Stack web developer
3 Vinay Graphic Designer
4 Rohan Frontend Developer
<?php
$students = array (
  array(1,"Aryan","Game Developer"),
  array(2,"Rankush","Full Stack web developer"),
  array(3,"Vinay","Graphic Designer"),
  array(4,"Rohan","Frontend Developer")
);
for ($row = 0; $row < 4; $row++) {
  echo "<p><b>Row number $row</b></p>";
  echo "<ul>";
  for ($col = 0; $col < 3; $col++) {
    echo "<li>".$students[$row][$col]."</li>";
  }
  echo "</ul>";
}
?>

Row number 0

  • 1
  • Aryan
  • Game Developer

Row number 1

  • 2
  • Rankush
  • Full Stack web developer

Row number 2

  • 3
  • Vinay
  • Graphic Designer

Row number 3

  • 4
  • Rohan
  • Frontend Developer

Now that you have done arrays in Php, let's learn constants in php


Share this page on :

=