PHP Associative Arrays
Postado 2024-05-17 08:01:14
0
5K
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to them.
ExampleGet your own PHP Server
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
var_dump($car);
Try it Yourself »Access Associative Arrays
To access an array item you can refer to the key name.
Example
Display the model of the car:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
echo $car["model"];
Try it Yourself »Change Value
To change the value of an array item, use the key name:
Example
Change the year
item:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
$car["year"] = 2024;
var_dump($car);
Try it Yourself »Loop Through an Associative Array
To loop through and print all the values of an associative array, you could use a foreach
loop, like this:
Example
Display all array items, keys and values:
$car = array("brand"=>"Ford", "model"=>"Mustang", "year"=>1964);
foreach ($car as $x => $y) {
echo "$x: $y <br>";
}
Try it Yourself »For a complete reference of all array functions, go to our complete PHP Array Reference.
PHP Exercises
Pesquisar
Categorias
- Art
- Causes
- Crafts
- Dance
- Drinks
- Film
- Fitness
- Food
- Jogos
- Gardening
- Health
- Início
- Literature
- Music
- Networking
- Outro
- Party
- Religion
- Shopping
- Sports
- Theater
- Wellness
Leia Mais
PHP Associative Arrays
PHP Associative Arrays
Associative arrays are arrays that use named keys that you assign to...
PHP Add Array Items
Add Array Item
To add items to an existing array, you can use the bracket [] syntax....
PHP if...else Statements
PHP - The if...else Statement
The if...else statement executes some code if a...
PHP Variables Scope
PHP Variables Scope
In PHP, variables can be declared anywhere in the script.
The scope of a...
PHP if Operators
Comparison Operators
If statements usually contain conditions that compare two values....