PHP array method collection

WBOY
Release: 2023-05-11 11:24:37
Original
492 people have browsed it

PHP is a programming language widely used in Web development. Its powerful array processing capabilities are part of its excellent performance. Through the flexible use of arrays, very complex data structures and algorithms can be easily implemented. This article will introduce some of the most commonly used array methods in PHP to help you better understand PHP's array functions and make your development process more efficient and convenient.

  1. array_diff() method

This method can be used to compare different values ​​in two arrays and return a new array containing different values. It receives two Or an array as a parameter, the syntax is as follows:

array_diff ( array $array1 , array $array2 [, array $... ] ) : array
Copy after login

Sample code:

$array1 = array("red","blue","green");
$array2 = array("red","yellow","blue");
$result = array_diff($array1, $array2);
print_r($result);
Copy after login

Output result:

Array
(
    [1] => green
)
Copy after login

In this example, array_diff() The method returns different values ​​in $array1 and $array2. "green" in $array1 does not exist in $array2, so it is returned as a different value.

  1. array_filter() method

This method can be used to filter elements in an array. It receives an array as a parameter, specifies filtering rules through a callback function, and returns The new array, the syntax is as follows:

array_filter ( array $array [, callable $callback [, int $flag = 0 ]] ) : array
Copy after login

Sample code:

function myFilter($value) {
  return strlen($value) > 5;
}

$array = array("apple", "orange", "banana", "watermelon");
$result = array_filter($array, "myFilter");
print_r($result);
Copy after login

Output result:

Array
(
    [3] => watermelon
)
Copy after login

In this example, the array_filter() method passes the callback Function myFilter() filters the $array array, leaving only elements whose string length is greater than five characters, so only "watermelon" is retained.

  1. array_map() method

This method creates a new array by performing the same operation on each element in an array. It receives one or more arrays. As parameters, and a callback function, the syntax is as follows:

array_map ( callable $callback , array $array1 [, array $... ] ) : array
Copy after login

Sample code:

function myFunc($value) {
  return $value * $value;
}

$array = array(1, 2, 3, 4, 5);
$result = array_map("myFunc", $array);
print_r($result);
Copy after login

Output result:

Array
(
    [0] => 1
    [1] => 4
    [2] => 9
    [3] => 16
    [4] => 25
)
Copy after login

In this example, array_map()Method uses callback function myFunc()Performs an operation on each element in the $array array, and stores the results of all operations in a new array$result middle.

  1. array_merge() method

This method is used to merge two or more arrays together and return a new array that receives two or more Multiple arrays as parameters, the syntax is as follows:

array_merge ( array $array1 [, array $... ] ) : array
Copy after login

Sample code:

$array1 = array("red","green","blue");
$array2 = array("yellow","purple");
$result = array_merge($array1, $array2);
print_r($result);
Copy after login

Output result:

Array
(
    [0] => red
    [1] => green
    [2] => blue
    [3] => yellow
    [4] => purple
)
Copy after login

In this example, the array_merge() method will $array1 and $array2 are combined into a new array and the result is stored in $result.

  1. array_reduce() method

This method performs dimensionality reduction on the values ​​in the array, iterates the array according to the callback function, and reduces the result to a single value. It receives an array as a parameter and a callback function. The syntax is as follows:

array_reduce ( array $array , callable $callback [, mixed $initial = NULL ] ) : mixed
Copy after login

Sample code:

function myReduce($carry, $item) {
  $carry += $item;
  return $carry;
}

$array = array(1, 2, 3, 4, 5);
$result = array_reduce($array, "myReduce", 0);
echo $result;
Copy after login

Output result:

15
Copy after login

In this example, array_reduce The () method iterates over each element in the $array array, uses the callback function myReduce() to reduce the dimension of the array, and returns the result as a single value .

  1. array_search() method

This method is used to find a value in the array and return its corresponding key name. If it does not exist, it returns false. It receives An array and a value as parameters, the syntax is as follows:

array_search ( mixed $needle , array $haystack [, bool $strict = false ] ) : mixed
Copy after login

Sample code:

$array = array("red", "green", "blue");
$key = array_search("green", $array);
echo $key;
Copy after login

Output result:

1
Copy after login

In this example, array_search()Method searches for the value "green" in the $array array, and returns its corresponding key name 1 after finding it.

  1. array_slice() method

This method is used to select a segment of elements from the array and return a new array. It receives an array and a starting position and A length is used as a parameter, and the syntax is as follows:

array_slice ( array $array , int $offset [, int $length = NULL [, bool $preserve_keys = false ]] ) : array
Copy after login

Sample code:

$array = array("red", "green", "blue", "yellow", "purple");
$result = array_slice($array, 1, 3);
print_r($result);
Copy after login

Output result:

Array
(
    [0] => green
    [1] => blue
    [2] => yellow
)
Copy after login

In this example, the array_slice() method is selected $arrayThree elements starting from the second element in the array, store them in the new array $result.

Summary

In the development process, we often need to process some data collections. PHP's array function can help us easily implement these functions. This article introduces some commonly used array methods, includingarray_diff()array_filter()array_map()array_merge()array_reduce()array_search() and array_slice() methods, I hope these methods can help readers during the development process.

The above is the detailed content of PHP array method collection. For more information, please follow other related articles on the PHP Chinese website!

source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template