Array in php

The array is used to store values ​​of different types within a variable. If in ordinary words, arrays are such a variable, then they can do the work of multiple variables alone.

or

Multiple values ​​at the same variable in Array; store is kept.A variable can not store more than one value.The array () is used to create an Array.

PHP has three types for Array.

1.Indexed Array.
2.Associative Array.
3.MultiDimensional Array.

1.Indexed Array.

The index array starts with index 0 and the number of elements that occur is up to -1. The element of each array is seperate with the comma. The most important is that “multiple elements are stored in the indexed array.”
For example, you can see below.

Example:-

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Indexed Arrays</title>
</head>
<body>
<?php
$colors = array("Red", "Green", "Blue");
// Printing array structure
print_r($colors);
?>
</body>
</html> 

 

2.Associative Array:-

Associative Array is also similar to Indexed Array. In the associative array, numeric keys are not used. To define index => value is used with this sign. The element of each array is seperate with the comma. But in the associative array, the programmer defines the index.

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Associative Array</title>
</head>
<body>
<?php
$ages = array("Peter"=>22, "jack"=>32, "marce"=>28);
// Printing array structure
print_r($ages); 
?>
</body>
</html>

 

3.Multidimentsional Array:-

The array that contains 2 or more arrays, we call it MultiDimensional Array. Multidimensional Array is also called Nested Array.

<!DOCTYPE html>
<html lang="en">
<head>
<title>PHP Multidimensional Array</title>
</head>
<body>
<?php
// Define nested array
$contacts = array(
array(
"name" => "Peter Parker",
"email" => "peterparker@mail.com",
),
array(
"name" => "Clark Kent",
"email" => "clarkkent@mail.com",
),
array(
"name" => "Harry Potter",
"email" => "harrypotter@mail.com",
)
);
// Access nested value
echo "Peter Parker's Email-id is: " . $contacts[0]["email"];
?>
</body>
</html>