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
Sample code 1:
//使用array()函数创建索引数组 $fruits = array("apple", "banana", "orange"); //使用简写形式创建索引数组 $fruits = ["apple", "banana", "orange"];
Sample code 2:
//使用array()函数创建关联数组 $person = array("name" => "Tom", "age" => 25, "city" => "Beijing"); //使用简写形式创建关联数组 $person = ["name" => "Tom", "age" => 25, "city" => "Beijing"];
3. Access array elements
Sample code 3:
$fruits = ["apple", "banana", "orange"]; echo $fruits[0]; //输出:apple echo $fruits[1]; //输出:banana echo $fruits[2]; //输出:orange
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
Sample code 5:
$fruits = ["apple", "banana", "orange"]; $fruits[] = "grape"; //将"grape"添加到末尾 $fruits[1] = "pear"; //将"pear"替换索引为1的元素 print_r($fruits);
Sample code 6:
$person = ["name" => "Tom", "age" => 25]; $person["city"] = "Beijing"; //添加键值对 print_r($person);
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
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 . " "; }
Sample code 9:
$person = ["name" => "Tom", "age" => 25, "city" => "Beijing"]; //使用foreach循环遍历关联数组 foreach($person as $key => $value){ echo $key . ": " . $value . " "; }
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!