As a popular dynamic website development language, PHP naturally has a wide range of data types and data structures, among which arrays are one of the most common data structures. In PHP, arrays are used to store a set of related data elements, and these data elements can be of different data types. This article will delve into the basic concepts, usage and some practical skills of PHP arrays.
1. Basic concepts
PHP array is a variable that can store multiple values. Each value has a key and a corresponding value, called a "key-value pair". The keys of an array can be numeric or string. Numeric keys are usually used to represent an ordered set of elements, and string keys are used to represent an unordered set of elements.
PHP arrays have the following types:
2. Usage method
In PHP, there are two ways to create an array. One is created through the array() function, as shown below:
$fruits = array("apple", "banana", "orange");
Another way to create an array is to use square brackets [], as shown below:
$fruits = ["apple", "banana", "orange"];
To access the elements in the array, you need to use the key name. For indexed arrays, the keys are numbers, and for associative arrays, the keys are strings. As shown below:
$fruits = ["apple", "banana", "orange"]; echo $fruits[0]; //输出"apple" $person = ["name"=>"John", "age"=>30, "gender"=>"male"]; echo $person["name"]; //输出"John"
Modifying the elements in the array can be achieved by key name. As shown below:
$fruits = ["apple", "banana", "orange"]; $fruits[0] = "pear"; print_r($fruits); //输出Array ( [0] => pear [1] => banana [2] => orange )
Adding elements can be achieved by directly adding a new element or using the array_push() function. Deleting elements can be achieved using the unset() function or the array_pop() function. As shown below:
$fruits = ["apple", "banana", "orange"]; $fruits[] = "pear"; print_r($fruits); //输出Array ( [0] => apple [1] => banana [2] => orange [3] => pear ) unset($fruits[2]); print_r($fruits); //输出Array ( [0] => apple [1] => banana [3] => pear )
Traversing the array can be implemented using a for loop or a foreach loop. As shown below:
$fruits = ["apple", "banana", "orange"]; for($i=0; $i Copy after login
The output result is:
apple banana orange apple banana orange
3. Practical skills
Yes Use the empty() function or count() function to determine whether the array is empty. The empty() function is used to determine whether a variable is empty, and the count() function is used to return the number of array elements. As shown below:
$fruits = ["apple", "banana", "orange"]; if(empty($fruits)){ echo "数组为空"; }else{ echo "数组不为空"; } echo "\n"; if(count($fruits)==0){ echo "数组为空"; }else{ echo "数组不为空"; }
The output result is:
数组不为空 数组不为空
You can use operators or array_merge() function to merge two arrays . As shown below:
$fruits1 = ["apple", "banana", "orange"]; $fruits2 = ["pear", "grape", "kiwi"]; $fruits = $fruits1 + $fruits2; print_r($fruits); //输出Array ( [0] => apple [1] => banana [2] => orange [3] => pear [4] => grape [5] => kiwi ) $fruits = array_merge($fruits1, $fruits2); print_r($fruits); //输出Array ( [0] => apple [1] => banana [2] => orange [3] => pear [4] => grape [5] => kiwi )
You can use the sort() function, rsort() function or usort() function to sort the array. The sort() and rsort() functions are used to sort index arrays in ascending and descending order, and the usort() function is used to sort associative arrays. As shown below:
$fruits = ["apple", "banana", "orange"]; sort($fruits); print_r($fruits); //输出Array ( [0] => apple [1] => banana [2] => orange ) rsort($fruits); print_r($fruits); //输出Array ( [0] => orange [1] => banana [2] => apple ) $person1 = ["name"=>"John", "age"=>30]; $person2 = ["name"=>"Tom", "age"=>20]; $person3 = ["name"=>"Alice", "age"=>25]; $people = [$person1, $person2, $person3]; function cmp($a, $b){ return $a["age"] - $b["age"]; } usort($people, "cmp"); print_r($people); //输出Array ( [0] => Array ( [name] => Tom [age] => 20 ) [1] => Array ( [name] => Alice [age] => 25 ) [2] => Array ( [name] => John [age] => 30 ) )
4. Summary
PHP array is a powerful and flexible data structure. Through the introduction of this article, we have learned about the basic concepts, usage methods and some practical skills of PHP arrays, which can help us use PHP arrays more skillfully to complete various tasks in website development.
The above is the detailed content of How to use php arrays. For more information, please follow other related articles on the PHP Chinese website!