In PHP, use the array() function to initialize an array. The syntax format is as follows:
$array_name = array(value1, value2, ..., valueN);
Among them, $array_name is the array name, and value1 to valueN are optional array elements. For example, the following code demonstrates how to use the array() function to initialize an array containing three elements:
$fruits = array("apple", "banana", "orange");
In addition, the array() function also supports the following ways to initialize the array:
Use key => ; The form of value, that is, the subscript and value of the array element are separated by "=>", as shown below:
$array_name = array(key1 => value1, key2 => value2, ..., keyN => valueN);
For example, the following is an example of using key => value to initialize an array:
$scores = array("Math" => 90, "English" => 80, "Chinese" => 95);
In addition, you can also use the range() function to generate integers or characters within a certain range and use them as array elements. The syntax format of the range() function is as follows:
array range ( mixed $start , mixed $end [, number $step = 1 ] )
Among them, $start and $end are the start and end values of the range, and $step is an optional parameter, indicating the step, that is, the interval for increasing the value. For example, the following code uses the range() function to generate integers from 1 to 5 and stores them in the array $numbers:
$numbers = range(1, 5);
Finally, one thing to note is that the subscripts of PHP arrays do not necessarily start from 0 , it is not necessary to only use numbers as subscripts, you can also use strings, Boolean, floating point and other types as subscripts. Therefore, when initializing an array, you can flexibly use various types of data as array subscripts.
The above is the detailed content of How to initialize an array with new in php. For more information, please follow other related articles on the PHP Chinese website!

This article explores efficient PHP array deduplication. It compares built-in functions like array_unique() with custom hashmap approaches, highlighting performance trade-offs based on array size and data type. The optimal method depends on profili

This article explores PHP array deduplication using key uniqueness. While not a direct duplicate removal method, leveraging key uniqueness allows for creating a new array with unique values by mapping values to keys, overwriting duplicates. This ap

This article analyzes PHP array deduplication, highlighting performance bottlenecks of naive approaches (O(n²)). It explores efficient alternatives using array_unique() with custom functions, SplObjectStorage, and HashSet implementations, achieving

This article details implementing message queues in PHP using RabbitMQ and Redis. It compares their architectures (AMQP vs. in-memory), features, and reliability mechanisms (confirmations, transactions, persistence). Best practices for design, error

This article examines current PHP coding standards and best practices, focusing on PSR recommendations (PSR-1, PSR-2, PSR-4, PSR-12). It emphasizes improving code readability and maintainability through consistent styling, meaningful naming, and eff

This article explores optimizing PHP array deduplication for large datasets. It examines techniques like array_unique(), array_flip(), SplObjectStorage, and pre-sorting, comparing their efficiency. For massive datasets, it suggests chunking, datab

This article details installing and troubleshooting PHP extensions, focusing on PECL. It covers installation steps (finding, downloading/compiling, enabling, restarting the server), troubleshooting techniques (checking logs, verifying installation,

This article explains PHP's Reflection API, enabling runtime inspection and manipulation of classes, methods, and properties. It details common use cases (documentation generation, ORMs, dependency injection) and cautions against performance overhea


Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

SublimeText3 Linux new version
SublimeText3 Linux latest version

Notepad++7.3.1
Easy-to-use and free code editor

Atom editor mac version download
The most popular open source editor

WebStorm Mac version
Useful JavaScript development tools

ZendStudio 13.5.1 Mac
Powerful PHP integrated development environment
