Creating an Array with the array() Function in PHP
This article explains how to create arrays in PHP using the array() function, detailing syntax, parameter description, optional trailing commas, and provides a comprehensive example with nested arrays and the resulting output.
Creating an array in PHP using the array() function.
Explanation: This creates an array. For more information about arrays, refer to the array section.
Parameter description: The syntax is "index => values", separated by commas. Indices can be strings or numbers. If omitted, integer indices start from 0. If an index is integer, the next generated index will be the current maximum integer index plus one. Duplicate indices cause later values to overwrite earlier ones.
Trailing commas after the last array element are legal, though uncommon.
Return value: Returns an array built from the given parameters. Indices can be supplied using the => operator.
Example:
<?php
$fruits = array(
"fruits" => array(
"a" => "orange",
"b" => "banana",
"c" => "apple"
),
"numbers" => array(1, 2, 3, 4, 5, 6),
"holes" => array(
"first" => 0,
"second" => 5,
"third" => 6
)
);
?>Output:
Array
[
fruits] => Array
[
a] => orange
b] => banana
c] => apple
]
[numbers] => Array
[
0] => 1
1] => 2
2] => 3
3] => 4
4] => 5
5] => 6
]
[holes] => Array
[
0] => first
5] => second
6] => third
]Signed-in readers can open the original source through BestHub's protected redirect.
This article has been distilled and summarized from source material, then republished for learning and reference. If you believe it infringes your rights, please contactand we will review it promptly.
Laravel Tech Community
Specializing in Laravel development, we continuously publish fresh content and grow alongside the elegant, stable Laravel framework.
How this landed with the community
Was this worth your time?
0 Comments
Thoughtful readers leave field notes, pushback, and hard-won operational detail here.
