An array stores multiple values in one single variable:
Example
Try it Yourself »
What is an Array?
An array is a special variable, which can hold more than one value at a time.
If you have a list of items [a list of car names, for example], storing the cars in single variables could look like this:
$cars1 = "Volvo";
$cars2 = "BMW";
$cars3 = "Toyota";
However, what if you want to loop through the cars and find a specific one? And what if you had not 3 cars, but 300?
The solution is to create an array!
An array can hold many values under a single name, and you can access the values by referring to an index number.
Create an Array in PHP
In PHP, the array[]
function is used to create an 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
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:
Example
Try it Yourself »
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!
PHP Exercises
❮ PHP Array Reference
Example
Create an indexed array named $cars, assign three elements to it, and then print a text containing the array values:
Try it Yourself »
Definition and Usage
The array[] function is used to create an array.
In PHP, there are three types of arrays:
- Indexed arrays - Arrays with numeric index
- Associative arrays - Arrays with named keys
- Multidimensional arrays - Arrays containing one or more arrays
Syntax
Syntax for indexed arrays:
array[value1, value2, value3, etc.]
Syntax for associative arrays:
array[key=>value,key=>value,key=>value,etc.]
Parameter Values
key | Specifies the key [numeric or string] |
value | Specifies the value |
Technical Details
Returns an array of the parameters |
4+ |
As of PHP 5.4, it is possible to use a short array syntax, which replaces array[] with []. E.g. $cars=["Volvo","BMW"]; instead of $cars=array["Volvo","BMW"]; |
More Examples
Example
Create an associative array named $age:
Try it Yourself »
Example
Loop through and print all the values of an indexed array:
Try it Yourself »
Example
Loop through and print all the values of an associative array:
Try it Yourself »
Example
Create a multidimensional array:
Try it Yourself »
❮ PHP Array Reference
Hướng dẫn dùng injs JavaScript
Hiện nay, JavaScript là một ngôn ngữ đang được nhiều bạn trẻ quan tâm nhiều trong những năm gần đây. Vậy JavaScript là gì? Và việc Tất cả sẽ có đầy ...
Hướng dẫn dùng value 1 trong PHP
Điểm hấp dẫn nhất của PHP theo mình là Array, và hầu như trong code, mọi thứ đều là key => value. Do vậy mà bạn biết thêm những hàm built-in rẳng của PHP, ...
Tạo thông báo trong php
Home » CodeAlert php được sử dụng trong trang web để hiển thị thông báo cảnh báo cho người dùng rằng họ đã nhập sai giá trị khác với giá trị được yêu ...
Hướng dẫn php oop form submit
Làm việc với form trong php gồm các vấn đề: sử dụng $_GET, $_POST ; hiện lại giá trị ; kiểm tra dữ liệu và hiện lỗi ; xử lý file upload; ứng dụng của ...
Hướng dẫn focus css
Trang chủTham khảoCSS:focusĐịnh nghĩa và sử dụng:focus thành phần sẽ focus khi được chọn.Cấu trúctag:focus { property: value; }Ví dụHTML viết: