PHP Create Arrays
نشر بتاريخ 2024-05-17 08:01:55
0
3كيلو بايت
Create Array
You can create arrays by using the array()
function:
You can also use a shorter syntax by using the []
brackets:
Multiple Lines
Line breaks are not important, so an array declaration can span multiple lines:
Trailing Comma
A comma after the last item is allowed:
Array Keys
When creating indexed arrays the keys are given automatically, starting at 0 and increased by 1 for each item, so the array above could also be created with keys:
As you can see, indexed arrays are the same as associative arrays, but associative arrays have names instead of numbers:
Declare Empty Array
You can declare an empty array first, and add items to it later:
The same goes for associative arrays, you can declare the array first, and then add items to it:
Example
$myCar = [];
$myCar["brand"] = "Ford";
$myCar["model"] = "Mustang";
$myCar["year"] = 1964;
Try it Yourself »Mixing Array Keys
You can have arrays with both indexed and named keys:
Example
$myArr = [];
$myArr[0] = "apples";
$myArr[1] = "bananas";
$myArr["fruit"] = "cherries";
Try it Yourself »البحث
الأقسام
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- الألعاب
- Gardening
- Health
- الرئيسية
- Literature
- Music
- Networking
- أخرى
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness
إقرأ المزيد
PHP if Statements
Conditional statements are used to perform different actions based on different conditions....
PHP Installation
What Do I Need?
To start using PHP, you can:
Find a web host with PHP and MySQL support...
PHP Comments
Comments in PHP
A comment in PHP code is a line that is not executed as a part of the program....
PHP Access Arrays
Access Array Item
To access an array item, you can refer to the index number for indexed arrays,...
PHP Multidimensional Arrays
In the previous pages, we have described arrays that are a single list of key/value pairs....