PHP Tutorial
PHP Arrays
An array in PHP is a data structure that can store multiple values by associating values to keys.
A PHP array can be defined by using the array()
function or the short array syntax []
.
Indexed Arrays
This type of array associates a numeric key or index with the value.
None
Associative Arrays
This type of array associates named keys with the value.
None
"John",
"age" => 20,
);
var_dump($var);
//using the short array syntax
$var = [
"name"=> "John",
"age" => 20,
];
var_dump($var);
?>
Multidimensional Arrays
This type of array stores multiple arrays.
None
"john",
"age" => 20
),
array (
"name" => "Ade",
"age" => 16
)
);
var_dump($var);
//using the short array syntax
$var = [
[
"name" => "john",
"age" => 20
],
[
"name" => "Ade",
"age" => 16
]
];
var_dump($var);
?>
Accessing Array Elements
Elements in an array can be accessed by array[key]
.
None
"John",
"age" => 20
];
var_dump(associative_array["name"]);
//multidimensional array
$multidimensional_array = [
[
"name" => "John",
"age" => 20
],
[
"name" => "Ade",
"age" => 16
]
];
var_dump($multidimensional_array[0]["name"]);
?>
What You Should Know at the End of This Lesson
- You should know about the two ways to declare an array.
- You should know about the types of arrays.
- You should know how to access array elements.