PHP Array: Indexed,Associative, Multidimensional

index associative multidimensional array in php; Through this tutorial, you will learn about PHP Indexed, Associative, Multidimensional array with examples.

PHP Array: Indexed,Associative, Multidimensional

PHP Array: Indexed,Associative, Multidimensional

Basically PHP array is a special type of variable in which is used to collect multiple data in it.

In other words, An array is a special types of variable, which can hold more than one value at a time.

Create a New Array in PHP

You can use the below syntax for creating a new array in PHP:

array();

In PHP, there are three types of arrays:

  • Indexed arrays – Arrays with a numeric index
  • Associative arrays – Arrays with named keys
  • Multidimensional arrays – Arrays containing one or more arrays

PHP Indexed Arrays

Here, we will create indexed arrays in PHP:

$lang = array("PHP", "PYTHON", "JAVASCRIPT");

Example of Indexed Arrays:

In the below example, we will create an indexed array named $lang. The $lang is contained three values.

1
2
3
4
5
6
7
8
9
10
11
<!DOCTYPE html>
<html>
<title>Indexed Arrays In PHP</title>
<head></head>
<body>
    <?php
    $lang = array("PHP", "PYTHON", "JAVASCRIPT");
    echo "I like " . $lang[0] . ", " . $lang[1] . " and " . $lang[2] . ".";
    ?>
</body>
</html>

PHP Array Length:

PHP Count() function is used to calculate the length of the array. It will return the length of the array

1
2
3
4
<?php
$lang = array("PHP", "PYTHON", "JAVASCRIPT");
echo count($lang);
?>

Loop with Indexed Arrays In PHP

Here, you will learn, how you can use the for loop with an indexed array in PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
<!DOCTYPE html>
<html>
<title>Loop with Indexed Arrays In PHP</title>
<head></head>
<body>
   <?php
    $lang = array("PHP", "PYTHON", "JAVASCRIPT");
    $arrlen = count($lang);
 
    for($x = 0; $x < $arrlen; $x++) {
        echo $lang[$x];
        echo "<br>";
    }
    ?>
</body>
</html>

PHP Associative Arrays

Associative Arrays in PHP. Associative arrays are used to store key value pairs.

$subjects = array("PHP"=>"50", "PYTHON"=>"55", "JAVASCRIPT"=>"45");

Example of Associative Arrays:

For example, to store the marks of the different subjects in an array, a numerically indexed array would not be the best choice.

1
2
3
4
5
6
7
8
9
10
11
12
13
<!DOCTYPE html>
<html>
<title>Associative Arrays In PHP</title>
<head></head>
<body>
    <?php
    $subjects = array("PHP"=>"50", "PYTHON"=>"55", "JAVASCRIPT"=>"45");
    echo "PHP => " . $subjects['PHP'] . "<br>";
    echo "PYTHON => " . $subjects['PYTHON'] . "<br>";
    echo "JAVASCRIPT => " . $subjects['JAVASCRIPT'] . "<br>";
    ?>
</body>
</html>

For Loop with Associative Arrays In PHP

Here, you will learn, how you can use for loop with associative an array in PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
<!DOCTYPE html>
<html>
<title>For Loop with Associative Arrays In PHP</title>
<head></head>
<body>
    <?php
    $subjects = array("PHP"=>"50", "PYTHON"=>"55", "JAVASCRIPT"=>"45");
 
    foreach($subjects as $x => $sub_value) {
        echo "Key=" . $x . ", Value=" . $sub_value;
        echo "<br>";
    }
    ?>
</body>
</html>

Multidimensional Arrays In PHP

In PHP, A multidimensional array is an array containing one or more arrays in it.

We will create a multidimensional array:

$cars = array
   (
   array("Volvo",22,18),
   array("BMW",15,13),
   array("Saab",5,2),
   array("Land Rover",17,15)
   );

Example of Multidimensional Arrays

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
<!DOCTYPE html>
<html>
<title>Multidimensional Arrays In PHP</title>
<head></head>
<body>
    <?php
    $cars = array
      (
      array("Volvo",22,18),
      array("BMW",15,13),
      array("Saab",5,2),
      array("Land Rover",17,15)
      );
 
    echo $cars[0][0].": In stock: ".$cars[0][1].", sold: ".$cars[0][2].".<br>";
    echo $cars[1][0].": In stock: ".$cars[1][1].", sold: ".$cars[1][2].".<br>";
    echo $cars[2][0].": In stock: ".$cars[2][1].", sold: ".$cars[2][2].".<br>";
    echo $cars[3][0].": In stock: ".$cars[3][1].", sold: ".$cars[3][2].".<br>";
 
    ?>
</body>
</html>

For loop with Multidimensional Array in PHP:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
<!DOCTYPE html>
<html>
<title>For Loop with Multidimensional Arrays In PHP</title>
<head></head>
<body>
    <?php
    $cars = array
      (
      array("Volvo",22,18),
      array("BMW",15,13),
      array("Saab",5,2),
      array("Land Rover",17,15)
      );
    for ($row = 0; $row < 4; $row++) {
      echo "<p><b>Row number $row</b></p>";
      echo "<ul>";
      for ($col = 0; $col < 3; $col++) {
        echo "<li>".$cars[$row][$col]."</li>";
      }
      echo "</ul>";
    }
    ?>
</body>
</html>

Foreach Loop Through an Associative Array PHP

See the below example for foreach loop with an associative array in PHP

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
<html>
<head>
<title>Foreach Loop Through an Associative Array</title>
</head>
<body>
<?php
$character = array (name=>"Joe",
                    occupation=>"Programmer",
                    age=>30,
                    "Learned language "=>"Java"
);
foreach ( $character as $key=>$val ){
    print "$key = $val<br>";
}
?>
</body>
</html>

Foreach loop through multidimensional array in PHP

Foreach loop is mainly used for looping through array values. Here you will learn how you can use foreach loop with PHP arrays:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
<!DOCTYPE html>
<html>
<title>For Loop with Multidimensional Arrays In PHP</title>
<head></head>
<body>
<?php
// Multidimensional array
$superheroes = array(
    "spider-man" => array(
        "name" => "Peter Parker",
        "email" => "peterparker@mail.com",
    ),
    "super-man" => array(
        "name" => "Clark Kent",
        "email" => "clarkkent@mail.com",
    ),
    "iron-man" => array(
        "name" => "Harry Potter",
        "email" => "harrypotter@mail.com",
    )
);
  
// Printing all the keys and values one by one
$keys = array_keys($superheroes);
for($i = 0; $i < count($superheroes); $i++) {
    echo $keys[$i] . "{<br>";
    foreach($superheroes[$keys[$i]] as $key => $value) {
        echo $key . " : " . $value . "<br>";
    }
    echo "}<br>";
}
?>
 
</body>
</html>