Home >Backend Development >PHP Tutorial >PHP study notes: Use and operation of arrays

PHP study notes: Use and operation of arrays

WBOY
WBOYOriginal
2023-10-08 09:16:23731browse

PHP study notes: Use and operation of arrays

PHP study notes: The use and operation of arrays

Introduction:
Array is a commonly used data structure and an important data type in PHP . Mastering the use and operation of arrays can help us better organize and process data. This article will introduce the basic concepts of arrays, create and initialize arrays, access array elements, add and delete array elements, traverse and sort arrays, and other operations, and attach specific code examples.

1. Basic concept of array
Array is a special variable that can store multiple values. These values ​​can be of any type, such as strings, integers, floating point numbers, etc. In PHP, an array can be an indexed array or an associative array.

2. Create and initialize the array

  1. Create the index array:
    The index array is arranged in order, and each element has a unique index value. To create an indexed array, use the array() function or the short form [].

Sample code 1:

//使用array()函数创建索引数组
$fruits = array("apple", "banana", "orange");

//使用简写形式创建索引数组
$fruits = ["apple", "banana", "orange"];
  1. Create an associative array:
    Associative arrays are created by specifying key-value pairs, and each element has a key. and the corresponding value.

Sample code 2:

//使用array()函数创建关联数组
$person = array("name" => "Tom", "age" => 25, "city" => "Beijing");

//使用简写形式创建关联数组
$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];

3. Access array elements

  1. Access index array elements:
    You can access array elements through index values , the index value starts counting from 0.

Sample code 3:

$fruits = ["apple", "banana", "orange"];

echo $fruits[0]; //输出:apple
echo $fruits[1]; //输出:banana
echo $fruits[2]; //输出:orange
  1. Accessing associative array elements:
    Associative array elements can be accessed by key.

Sample code 4:

$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];

echo $person["name"]; //输出:Tom
echo $person["age"]; //输出:25
echo $person["city"]; //输出:Beijing

4. Adding and deleting array elements

  1. Add elements to the index array:
    You can use the index value to Adds an element to the end of an indexed array, or adds an element to a specified position by specifying an index value.

Sample code 5:

$fruits = ["apple", "banana", "orange"];

$fruits[] = "grape"; //将"grape"添加到末尾
$fruits[1] = "pear"; //将"pear"替换索引为1的元素

print_r($fruits);
  1. Add elements to associative arrays:
    You can use new key-value pairs to add elements to associative arrays.

Sample code 6:

$person = ["name" => "Tom", "age" => 25];

$person["city"] = "Beijing"; //添加键值对

print_r($person);
  1. Delete array elements:
    You can use the unset() function to delete the element at the specified position in the array, or use unset() Function removes the element with the specified key from an associative array.

Sample code 7:

$fruits = ["apple", "banana", "orange"];

unset($fruits[1]); //删除索引为1的元素

print_r($fruits);

$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];

unset($person["age"]); //删除键为"age"的元素

print_r($person);

5. Array traversal and sorting

  1. Traverse the index array:
    You can use a for loop or a foreach loop to iterate over the index array.

Sample code 8:

$fruits = ["apple", "banana", "orange"];

//使用for循环遍历索引数组
for($i = 0; $i < count($fruits); $i++){
    echo $fruits[$i] . " ";
}

//使用foreach循环遍历索引数组
foreach($fruits as $fruit){
    echo $fruit . " ";
}
  1. Traverse associative arrays:
    You can use a foreach loop to traverse associative arrays.

Sample code 9:

$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];

//使用foreach循环遍历关联数组
foreach($person as $key => $value){
    echo $key . ": " . $value . " ";
}
  1. Sorting of arrays:
    You can use the sort() function to sort the index array in ascending order, and use the rsort() function to sort the index Sort the array in descending order.

Sample code 10:

$numbers = [3, 1, 2];

sort($numbers); //升序排序

print_r($numbers);

rsort($numbers); //降序排序

print_r($numbers);

Conclusion:
This article introduces the basic concepts of arrays, creating and initializing arrays, accessing array elements, adding and deleting array elements, and Operations such as traversal and sorting are provided, and specific code examples are given. After mastering the use and operation of arrays, I believe readers can better use arrays to organize and process data in PHP programming.

The above is the detailed content of PHP study notes: Use and operation of arrays. 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