What are the two types of php arrays?

WBOY
Release: 2023-05-22 18:29:07
Original
606 people have browsed it

PHP is a very popular programming language that is widely used for web development. In PHP, array is an extremely important data type and is widely used in code. There are two types of arrays in PHP, namely indexed arrays and associative arrays.

  1. Index array

In PHP, index array is the most common. It is an array whose elements are indexed numerically. Think of it as an ordered list where each element has a unique numerical index.

Creating an index array in PHP is very simple, you can use the following syntax:

$array = array(value1, value2, value3, ...);
Copy after login

where value1, value2, value3,... are the elements in the array. For example, the following code creates an array with three elements:

$fruits = array("apple", "banana", "orange");
Copy after login

As mentioned above, each element has a numeric index, the first element has index 0 and the second element has index 0 1, and so on. We can access the corresponding element through the index of the array, for example:

echo $fruits[0]; // 输出:apple
echo $fruits[1]; // 输出:banana
echo $fruits[2]; // 输出:orange
Copy after login

In addition, we can also use a foreach loop to traverse all elements in the index array, for example:

foreach ($fruits as $fruit) {
  echo $fruit . " ";
}
// 输出:apple banana orange
Copy after login
  1. Associative Array

Associative array is another common type in PHP. It is an array whose elements are indexed using string keys. Associative arrays are commonly used to store data such as configuration files and database records.

The syntax for creating an associative array in PHP is as follows:

$array = array(
  key1 => value1,
  key2 => value2,
  key3 => value3,
  ...
);
Copy after login

where key1, key2, and key3 are string keys, and value1, value2, and value3 are the corresponding values. For example, the following code creates an associative array containing three elements:

$person = array(
  "name" => "John",
  "age" => 30,
  "city" => "New York"
);
Copy after login

In an associative array, each element has a unique string key, and we can get the value of the element through the key. For example:

echo $person["name"]; // 输出:John
echo $person["age"]; // 输出:30
echo $person["city"]; // 输出:New York
Copy after login

Similarly, we can also use a foreach loop to traverse associative arrays:

foreach ($person as $key => $value) {
  echo $key . ": " . $value . " ";
}
// 输出:name: John age: 30 city: New York
Copy after login

Summary

In PHP, arrays are a very important data type. It is divided into two types: index array and associative array. An indexed array is an ordered list whose element values ​​are obtained by numerical index. An associative array is an array indexed using string keys and is typically used to store data such as configurations and database records. Mastering these two types of arrays is one of the basic skills of PHP developers.

The above is the detailed content of What are the two types of php arrays?. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template