Home  >  Article  >  Backend Development  >  How to write php array

How to write php array

PHPz
PHPzOriginal
2023-04-24 14:48:54639browse

PHP is a very popular programming language and it is the most popular of the server-side scripting languages. PHP has a wealth of data types and structures, among which arrays are a very important data structure. In PHP, arrays can be used to store a series of related values ​​for developers to read and operate. Let's take a look at how to write PHP arrays.

Definition of array

To define an array in PHP, you can use the array() function or the square brackets []. For example:

// 使用array()函数定义数组 
$fruits = array("apple", "banana", "orange");

// 使用方括号[]定义数组 
$colors = ["red", "green", "blue"];

The above two methods define an array containing three elements, namely "apple", "banana" and "orange" in the $fruits array and $colors"red", "green" and "blue" in the array. It should be noted that the elements and keys in the array can be of any variable type.

Array access

You can use the key value of the element to access the elements in the array. In PHP, array subscripts start from 0 by default. For example, to access the first element in the $fruits array we can write:

echo $fruits[0]; // 输出 "apple"

We can also use the print_r() or var_dump() function to print the entire Array to view the structure and contents of the array. For example:

print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => orange )

Use the count() function of the array to get the number of elements in the array, for example:

echo count($fruits); // 输出 3

Addition and deletion of the array

In PHP, we can use normal assignment operators to add elements to the array, for example:

$fruits[3] = "grape"; // 添加一个元素
print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => orange [3] => grape )

We can also use the array_push() function to add elements to the end of the array, for example:

array_push($fruits, "kiwi"); // 添加一个元素
print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => orange [3] => grape [4] => kiwi )

If you need to delete elements in the array, you can use the unset() function. For example:

unset($fruits[2]); // 删除 orange 元素
print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [3] => grape [4] => kiwi )

It should be noted that when using the unset() function to delete array elements, the corresponding keys will also be deleted together. If you need to preserve the continuity of the keys, you can use array_values ()Function re-indexes the array. For example:

$fruits = array_values($fruits);
print_r($fruits); // 输出 Array ( [0] => apple [1] => banana [2] => grape [3] => kiwi )

Multidimensional array

In PHP, arrays can also be multidimensional and can contain any number of levels of array elements. For example:

$students = [
    ["name" => "Tom", "score" => 80],
    ["name" => "Jerry", "score" => 90],
    ["name" => "Bob", "score" => 70]
];

In the above example, the $students array contains three elements, each element is an associative array containing "name" and "score" elements.

Accessing elements in a multidimensional array can be accessed through nested subscripts. For example:

echo $students[0]["name"]; // 输出 "Tom"
echo $students[1]["score"]; // 输出 90

Summary

Through the above introduction, we can see the definition, access, addition, deletion of PHP arrays and the definition and access methods of multi-dimensional arrays. Mastering the basic operations of arrays can make it easier for us to organize and process data.

The above is the detailed content of How to write php array. 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