Home  >  Article  >  Backend Development  >  There are several ways to create an array in php

There are several ways to create an array in php

PHPz
PHPzOriginal
2023-05-19 13:13:07811browse

PHP is a very popular server-side scripting language, and developers can use a variety of methods to create arrays. This article will introduce several common ways to create arrays in PHP.

  1. Using array literals

In PHP, array literals are the most common way to create arrays. When using this method, we enter one or more elements directly in a pair of square brackets, each separated by commas. The code is as follows:

$fruits = ['apple', 'banana', 'orange'];

Among them, $fruits is an array composed of three string elements. This method is very intuitive and easy to use, so it is widely used in various development environments.

  1. Using the array() function

Another common way to create an array is to use the array() function . This function can accept any number of parameters, each parameter represents an element in the array. The code is as follows:

$numbers = array(1, 2, 3, 4, 5);

In this example, we use the array() function to create an array of five integer elements. This method is one of the earliest ways to create arrays in PHP. Although it has been replaced by many more intuitive and easier ways, it is still a good choice in some situations.

  1. Use range() function

If we need to create a numerical array within a certain range, we can use range()function. This function can receive three parameters: starting value, end value and step size. The code is as follows:

$numbers = range(1, 10, 2);

In this example, we use the range() function to create an array consisting of odd numbers between 1 and 10. This method is very simple and helps avoid manually entering a series of consecutive values.

  1. Use explode()Function

If we need to split a string into multiple elements and store it in an array , you can use the explode() function. This function can receive two parameters: the delimiter and the string to be split. The code is as follows:

$names = explode(', ', 'Alice, Bob, Carol');

In this example, we use commas and spaces as delimiters to split a string containing three names into an array of three elements. This method is very useful when parsing fixed format strings.

  1. Create from other arrays

There are many other ways in PHP to create new arrays from existing arrays. The more common ones are to use the array_slice() function or the array_filter() function. The code is as follows:

$numbers = [1, 2, 3, 4, 5];
$subset = array_slice($numbers, 2, 2);
$evens = array_filter($numbers, function($num) {
    return $num % 2 == 0;
});

In this example, we use the array_slice() function to select a part of the elements from an array, and the array_filter() function to select one of them All even-numbered elements. This method is very flexible. For example, we can sort the elements according to a certain rule or delete certain elements in the array as needed.

Summary

In PHP, we can use a variety of methods to create arrays. From using array literals directly, to using a function to split strings, and more. Each method has its advantages and limitations. Therefore, developers should choose the appropriate method based on specific scenarios and needs. At the same time, it is recommended to explore various ways to fully utilize the powerful functions of PHP.

The above is the detailed content of There are several ways to create an array in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn