Home  >  Article  >  Backend Development  >  How to use php arrays

How to use php arrays

PHPz
PHPzOriginal
2023-05-07 12:18:07432browse

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:

  1. Index array: the key is a number and the value is an ordered list.
  2. Associative array: The key is a string and the value is any data type.
  3. Multidimensional array: The elements of the array can also be arrays, which become multidimensional arrays.

2. Usage method

  1. Create array

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"];
  1. Access Elements in the array

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"
  1. Modify the elements in the array

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 )
  1. Adding and deleting elements in the array

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 )
  1. Traversing the array

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

The output result is:

apple banana orange
apple banana orange

3. Practical skills

  1. Judge whether the array is empty

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:

数组不为空
数组不为空
  1. Merge arrays

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 )
  1. Sort array

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!

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