Home  >  Article  >  Backend Development  >  How many types of arrays are there in php?

How many types of arrays are there in php?

PHPz
PHPzOriginal
2023-04-18 09:46:25564browse

In PHP language, array is a very common data type. It can be used to store a set of data. These data can be of different types, such as numbers, strings, Boolean values, etc. Arrays in PHP have great flexibility and functionality. You can access elements in the array through subscripts, or you can use loops to traverse the entire array. In PHP, there are three types of arrays: indexed arrays, associative arrays, and multidimensional arrays.

  1. Index array

Index array is the most common array type. In an indexed array, each array element has an ordered numeric subscript, which usually starts from 0 and increases in sequence. To create an indexed array, use the following syntax:

$array = array('apple', 'banana', 'orange');

The above code creates an indexed array containing three elements, each element has a numeric subscript, namely 0, 1, and 2. You can use subscripts to access elements in the array:

echo $array[0]; // 输出 "apple"
echo $array[1]; // 输出 "banana"
echo $array[2]; // 输出 "orange"

In PHP, there is a similar way of writing:

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

This way of writing is only used in PHP 5.4 or above. support.

  1. Associative array

Compared with the index array, the only difference between the associative array and the index array is that its subscript is no longer incremented by consecutive numbers, but can be customized. Each array element is corresponding to a string type key value. The way to create an associative array is as follows:

$info = array('name' => 'John', 'age' => 30, 'city' => 'New York');

The above code creates an associative array containing three elements. The key values ​​of each element are 'name', 'age' and 'city', and the corresponding values ​​are 'John', 30 and 'New York' respectively. You can use key values ​​to access array elements:

echo $info['name']; // 输出 "John"
echo $info['age']; // 输出 30
echo $info['city']; // 输出 "New York"

In versions above PHP 5.4, there is also a concise way of writing:

$info = ['name' => 'John', 'age' => 30, 'city' => 'New York'];
  1. Multidimensional array

Multidimensional array means that the elements in the index array are also an array. That is to say, the multidimensional array is composed of multiple one-dimensional arrays. There are also two ways to create multi-dimensional arrays in PHP:

// 第一种方式
$products = array(
    array('name' => 'iPhone', 'price' => 699),
    array('name' => 'iPad', 'price' => 999),
    array('name' => 'iPod', 'price' => 299)
);

// 第二种方式
$products = [
    ['name' => 'iPhone', 'price' => 699],
    ['name' => 'iPad', 'price' => 999],
    ['name' => 'iPod', 'price' => 299]
];

The above code creates a multi-dimensional array containing three elements, each element is a one-dimensional array. You can use multiple indexes to access array elements:

echo $products[0]['name']; // 输出 "iPhone"
echo $products[1]['price']; // 输出 999
echo $products[2]['name']; // 输出 "iPod"

You can also use multiple key values ​​to access array elements:

echo $products[0]['name']; // 输出 "iPhone"
echo $products[1]['price']; // 输出 999
echo $products[2]['name']; // 输出 "iPod"

Summary

Arrays in PHP are a very important A data type that can be used to store a set of data. In PHP, there are three array types: indexed arrays, associative arrays, and multidimensional arrays. Indexed arrays are the simplest and most common array type, with each element having a numeric subscript. Each element of an associative array has a custom key value. Multidimensional arrays are composed of multiple one-dimensional arrays, and each element is a one-dimensional array. Understanding the types and usage of arrays in PHP is very important for developing PHP applications.

The above is the detailed content of How many types of arrays are there 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