PHP Create Arrays
~1 min read
·
PHP
You can create arrays in a variety of ways. Arrays are essential data structures for grouping multiple values in a single variable.
Create Array
Using the array() function:
Example
<pre >
<?php
$fruits = array ("Apple" , "Banana" , "Cherry" );
var_dump($fruits);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Using short array syntax ( [ ] ):
You can also use a shorter syntax by using the [] brackets:
Example
<pre >
<?php
$fruits = ["Apple" , "Banana" , "Cherry" ];
var_dump($fruits);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Array Keys When creating indexed arrays the keys are given automatically to store values, starting at 0 and increased by 1 for each item, so the array above could also be created with keys:
Example
< pre >
<?php
$cars = [
0 => "Volvo" ,
1 => "BMW" ,
2 =>"Toyota"
];
var_dump($cars);
?>
</pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
As you can see, indexed arrays are the same as associative arrays, but associative arrays uses custom keys (which can be strings or integers) to store values:
Example
< pre >
<?php
$person = array (
"name" => "John" ,
"age" => 25 ,
"city" => "New York"
);
var_dump($person);
?>
</pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Creating an Empty Array
You can also create an empty array and add elements to it later.
Example
<pre >
<?php
$emptyArray = [];
$emptyArray[] = "Item 1" ;
$emptyArray[] = "Item 2" ;
var_dump($emptyArray);
?>
</ pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
The same goes for associative arrays, you can declare the array first, and then add items to it:
Example
< pre >
<?php
$student = [];
$student["name" ] = "Alice" ;
$student["age" ] = 22 ;
$student["major" ] = "Computer Science" ;
var_dump($student);
?>
</pre >
show = true, 3000)" class="absolute top-[10px] right-[10px] inline-flex items-center justify-center gap-2 text-sm font-medium hover:bg-accent hover:text-accent-foreground transition-smooth h-9 rounded-md px-3 cursor-pointer bg-white/80 border border-border">
Try it yourself
Note
Array can have both indexed and named keys.