Home >Backend Development >PHP Tutorial >How to create a new array in PHP

How to create a new array in PHP

王林
王林forward
2024-03-19 22:30:07707browse

php editor Baicao teaches you how to create a new array. In PHP, you can use the array() function or square brackets [] to create an array. For example, using the array() function, you can write like this: $arr = array('apple', 'banana', 'orange'); or using square brackets [], you can write like this: $arr = ['apple', 'banana' , 'orange'];This successfully creates an array containing three elements. If you need to create an associative array, you can use the form of key-value pairs: $arr = array('name' => 'Alice', 'age' => 25); or: $arr = ['name' => 'Alice ', 'age' => 25];This creates an associative array.

PHP Create Array

An array is a data structure used to store multiple values ​​ sorted by index . php Provides multiple methods to create arrays.

1. Index array

Indexed arrays use integer indexes to access elements.

$fruits = ["Apple", "Banana", "Orange"];

In the above example, the first element in the array has index 0, the second element has index 1, and so on.

2. Associative array

Associative arrays use string keys instead of integer indexes to access elements.

$person = ["name" => "John Doe", "age" => 30, "city" => "New York"];

In the above example, "name", "age" and "city" are the keys of the array.

3. Multidimensional array

A multidimensional array is an array that contains one or more other arrays.

$data = [
["name" => "John Doe", "age" => 30],
["name" => "Jane Doe", "age" => 25]
];

In the above example, $data is a multidimensional array containing two associative arrays.

4. Array function

PHP provides several functions to create and manipulate arrays.

  • array(): Creates an array and returns a reference to the array.
  • array_merge(): Merge two or more arrays.
  • array_slice(): Extract a range of elements from the array.
  • array_fill(): Fill an array with specific values.
  • array_keys(): Returns an array of all keys in the array.
  • array_values(): Returns an array of all values ​​in the array.
  • array_flip(): Exchange the keys and values ​​in the array.
  • array_intersect(): Returns the elements present in two arrays.
  • array_diff(): Returns elements in the first array that do not exist in the second array.

5. Short Syntax

Starting with PHP 5.4, a short syntax was introduced to create arrays.

  • Index array: ["Apple", "Banana", "Orange"]
  • Associative array: ["name" => "John Doe", "age" => 30]

Best Practices

  • Always specify type hints for elements in arrays.
  • Use appropriate key names to make the array structure easy to understand.
  • Avoid using zero-indexed associative arrays.
  • Consider using the SplFixedArray class to improve performance on large arrays.

The above is the detailed content of How to create a new array in PHP. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:lsjlt.com. If there is any infringement, please contact admin@php.cn delete