Home > Article > Backend Development > How to create an array in php
PHP is a popular server-side programming language that provides a series of array functions that make arrays very easy to handle in PHP. Creating an array in PHP is very simple and there are different ways to create different types of arrays. In this article, we will explore how to create arrays using PHP.
1. Create a numerical array
Numeric array is the most basic array type. The elements in the array are arranged in order, and each element has a numerical index. In PHP, you can use the array() function to create a new numeric array, as shown below:
$myArray = array(1, 2, 3, 4, 5);
In the above example, $myArray is a numeric array containing 5 elements, each element There is a numerical index.
In addition to using the array() function, you can also use square brackets [] to create a numerical array, as shown below:
$myArray = [1, 2, 3, 4, 5];
Both methods can create a numerical array, both can be done Operations like adding, modifying, and deleting elements.
2. Create an associative array
Associative array is a more flexible and easier-to-use array type. In an associative array, each element has a unique string key, and the element can be accessed using that key. In PHP, you can use the array() function to create a new associative array, as shown below:
$myArray = array( "name" => "John", "age" => 30, "email" => "john@example.com" );
In the above example, $myArray is an associative array containing three elements. Each element has a string key, such as "name", "age", and "email".
In addition to using the array() function, you can also use square brackets [] to create associative arrays, as shown below:
$myArray = [ "name" => "John", "age" => 30, "email" => "john@example.com" ];
No matter which method is used, these arrays are associative arrays, and Elements can be accessed using keys.
3. Create a multidimensional array
A multidimensional array is an array composed of arrays. That is to say, in a multidimensional array, each element is an array. In PHP, you can use the array() function to create a new multi-dimensional array as follows:
$myArray = array( array(1, 2, 3), array("John", "Mary", "Peter"), array("email" => "john@example.com", "phone" => "123456789") );
In the above example, $myArray is a multi-dimensional array containing three elements. Each element is an array.
In addition to using the array() function, you can also use square brackets [] to create a multi-dimensional array, as shown below:
$myArray = [ [1, 2, 3], ["John", "Mary", "Peter"], ["email" => "john@example.com", "phone" => "123456789"] ];
Both methods can create a multi-dimensional array, and multi-dimensional arrays can Infinitely expandable.
4. Add elements to the array
In PHP, you can use the array_push() function or directly use [] to add elements to the array. For example, to add new elements to a numeric array, you can use the following code:
$myArray = [1, 2, 3]; $myArray[] = 4; array_push($myArray, 5);
In the above example, two methods are used to add new elements to the array. In the end, $myArray contains 5 elements.
To add new elements to an associative array, you can use the following code:
$myArray = ["name" => "John", "age" => 30]; $myArray["email"] = "john@example.com";
In the above example, square brackets [] are used to add new elements. Ultimately, $myArray contains 3 elements.
5. Delete elements from an array
In PHP, you can use the unset() function or array_splice() function to delete elements from an array. For example, to delete elements from a numeric array, you can use the following code:
$myArray = [1, 2, 3, 4, 5]; unset($myArray[2]); array_splice($myArray, 1, 1);
In the above example, two methods are used to delete elements from the array. In the end, $myArray contains 3 elements.
To delete elements in an associative array, you can use the following code:
$myArray = ["name" => "John", "age" => 30, "email" => "john@example.com"]; unset($myArray["age"]);
In the above example, the unset() function is used to delete an element. In the end, $myArray contains two elements. .
6. Traverse the array
In PHP, you can use the foreach() loop to traverse each element in the array and operate on each element. For example, to traverse a numerical array, you can use the following code:
$myArray = [1, 2, 3, 4, 5]; foreach($myArray as $value) { echo $value . "<br>"; }
In the above example, a foreach() loop is used to traverse the elements in the array and operate on each element.
To traverse an associative array, you can use the following code:
$myArray = ["name" => "John", "age" => 30, "email" => "john@example.com"]; foreach($myArray as $key => $value) { echo $key . ": " . $value . "<br>"; }
In the above example, a foreach() loop is used to traverse the elements in the array, where $key represents the key of the array element, $value represents the value of the array element.
To sum up, creating arrays in PHP is very simple, but for arrays of different types, different dimensions and different scenarios, you may need to use different ways to create and operate them. At the same time, PHP provides a series of array functions to make array processing more flexible and efficient.
The above is the detailed content of How to create an array in php. For more information, please follow other related articles on the PHP Chinese website!