Home  >  Article  >  Backend Development  >  How to operate on arrays in php

How to operate on arrays in php

PHPz
PHPzOriginal
2023-04-25 18:22:10773browse

PHP is a popular programming language widely used for web development. Array is one of the most important data types in PHP. It is very convenient and flexible and can accommodate any type of data, including numbers, strings, objects, etc. In PHP, there are many built-in array functions that can perform various operations on arrays, including sorting, searching, merging, filtering, etc. This article will introduce some commonly used array functions and give corresponding code implementations.

Creation and modification of arrays

In PHP, we can use the array function to create an empty array, and add elements to it by index or key value, as follows:

$empty_array = array();

$indexed_array = array('apple', 'banana', 'orange');

$assoc_array = array('name' => 'John', 'age' => 20, 'gender' => 'male');

Among them, $empty_array is an empty array, $indexed_array is an indexed array, $assoc_array is an associative array, you can also use square brackets and subscripts to access and modify array elements:

$indexed_array[0] = 'pear';

$assoc_array['name'] = 'Mike';

Array traversal

Traversing an array is a very common operation. We can use a loop to traverse the array. PHP provides a foreach loop structure for traversing arrays:

foreach ($indexed_array as $value) {
    echo $value . ' ';
}
//输出:pear banana orange

foreach ($assoc_array as $key => $value) {
    echo $key . ': ' . $value . ' ';
}
//输出:name: Mike age: 20 gender: male

Sort of arrays

Sort is one of the commonly used operations on arrays. In PHP, you can use functions such as sort, rsort, asort, arsort, ksort and krsort to sort arrays, as follows:

$numbers = array(5, 3, 7, 1, 9);

sort($numbers);
print_r($numbers);
//输出:Array ( [0] => 1 [1] => 3 [2] => 5 [3] => 7 [4] => 9 )

rsort($numbers);
print_r($numbers);
//输出:Array ( [0] => 9 [1] => 7 [2] => 5 [3] => 3 [4] => 1 )

$fruits = array('apple', 'banana', 'pear', 'orange');

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

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

$ages = array('John' => 20, 'Mike' => 30, 'Lisa' => 25);

ksort($ages);
print_r($ages);
//输出:Array ( [John] => 20 [Lisa] => 25 [Mike] => 30 )

krsort($ages);
print_r($ages);
//输出:Array ( [Mike] => 30 [Lisa] => 25 [John] => 20 )

Array search

Search is one of the commonly used operations on arrays one. In PHP, you can use functions such as in_array, array_search and array_key_exists to search for array elements, as follows:

$fruits = array('apple', 'banana', 'pear', 'orange');

if (in_array('banana', $fruits)) {
    echo 'banana exists';
}
//输出:banana exists

$key = array_search('pear', $fruits);
echo 'pear is at index ' . $key;
//输出:pear is at index 2

$ages = array('John' => 20, 'Mike' => 30, 'Lisa' => 25);

if (array_key_exists('John', $ages)) {
    echo 'John exists';
}
//输出:John exists

Merge of arrays

Merge is one of the commonly used operations on arrays. In PHP, you can use array_merge, array_replace and operators to merge arrays, as follows:

$a = array('red', 'green', 'blue');
$b = array('yellow', 'orange', 'purple');
$c = array_merge($a, $b);
print_r($c);
//输出:Array ( [0] => red [1] => green [2] => blue [3] => yellow [4] => orange [5] => purple )

$a = array('name' => 'Mike', 'age' => 30);
$b = array('age' => 25, 'gender' => 'male');
$c = array_replace($a, $b);
print_r($c);
//输出:Array ( [name] => Mike [age] => 25 [gender] => male )

$a = array('red', 'green', 'blue');
$b = array('yellow', 'orange', 'purple');
$c = $a + $b;
print_r($c);
//输出:Array ( [0] => red [1] => green [2] => blue [3] => yellow [4] => orange [5] => purple )

Filtering of arrays

Filtering is one of the commonly used operations on arrays. In PHP, you can use functions such as array_filter and array_map to filter arrays, as follows:

$numbers = array(5, -3, 7, -1, 9);

$positive_numbers = array_filter($numbers, function($number) {
    return $number > 0;
});
print_r($positive_numbers);
//输出:Array ( [0] => 5 [2] => 7 [4] => 9 )

$squares = array_map(function($number) {
    return $number * $number;
}, $numbers);
print_r($squares);
//输出:Array ( [0] => 25 [1] => 9 [2] => 49 [3] => 1 [4] => 81 )

This article introduces some array functions commonly used in PHP and provides corresponding code implementations. These functions can greatly improve the efficiency of our array operations, allowing us to process and manage data more conveniently. For beginners, using these functions can also improve the readability and maintainability of the code and avoid unnecessary errors.

The above is the detailed content of How to operate on arrays in php. 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