Home  >  Article  >  Backend Development  >  How to traverse arrays and compare values ​​in php

How to traverse arrays and compare values ​​in php

PHPz
PHPzOriginal
2023-04-18 09:05:01545browse

In PHP, traversing arrays and comparing values ​​in arrays are very common operations. Below we will introduce several methods to traverse arrays and compare values ​​in arrays.

1. Traverse an array

  1. for loop

The for loop is one of the most commonly used methods of traversing an array in PHP. The format of the for loop is as follows:

for ($i=0; $i<count($array); $i++) {
    // 获取数组中的值
}

In the above code, we use the for loop to traverse the array $array, $i represents the number of current loops, the initial value of $i is 0, and each time it loops $i The value is incremented by 1, and the number of loops is the length of the array ($i

In the loop body, we can use $array[$i] to get the value corresponding to the current subscript in the array, for example:

$array = array(1, 2, 3, 4, 5);
for ($i=0; $i<count($array); $i++) {
    echo $array[$i] . &#39; &#39;;
}
// 输出:1 2 3 4 5
  1. foreach loop

Another commonly used method of traversing arrays in PHP is the foreach loop, with the following format:

foreach ($array as $value) {
    // 获取数组中的值
}

In the above code, $array represents the array to be traversed, and $value represents the value of the current loop.

When using the foreach loop to traverse the array, we do not need to calculate the length of the array. The foreach loop can also traverse the associative array.

For example:

$array = array(&#39;apple&#39; => 'red', 'banana' => 'yellow', 'orange' => 'orange');
foreach ($array as $key => $value) {
    echo $key . ' is ' . $value . '<br>';
}
// 输出:
// apple is red
// banana is yellow
// orange is orange

2. Compare values ​​in arrays

  1. in_array() function

PHP provides in_array( ) function to determine whether a value exists in an array. The format of the in_array() function is as follows:

in_array($value, $array)

In the above code, $value represents the value to be compared, and $array represents the array to be compared.

The in_array() function returns true if $value exists in $array, otherwise it returns false.

For example:

$array = array('apple', 'banana', 'orange');
if (in_array('banana', $array)) {
    echo 'banana exists in the array';
} else {
    echo 'banana does not exist in the array';
}
// 输出:banana exists in the array
  1. array_search() function

Similar to the in_array() function, PHP also provides the array_search() function to find an The position of the value in the array. The format of the array_search() function is as follows:

array_search($value, $array)

In the above code, $value represents the value to be searched, and $array represents the array to be searched.

If $value exists in $array, the array_search() function returns the key name of the first occurrence of the value in the array, otherwise it returns false.

For example:

$array = array('apple', 'banana', 'orange');
$find = array_search('banana', $array);
if ($find !== false) {
    echo 'banana is at position ' . $find . ' in the array';
} else {
    echo 'banana does not exist in the array';
}
// 输出:banana is at position 1 in the array
  1. array_diff() function

The array_diff() function in PHP is used to compare the difference between two arrays and return the difference value. The format of the array_diff() function is as follows:

array_diff($array1, $array2)

In the above code, $array1 and $array2 respectively represent the two arrays to be compared.

array_diff() function will compare the value in $array1 with the value in $array2, find the elements that exist in $array1 but not in $array2, and return a new array composed of these elements.

For example:

$array1 = array('apple', 'banana', 'orange');
$array2 = array('banana', 'grape', 'pear');
$result = array_diff($array1, $array2);
print_r($result);
// 输出:Array ( [0] => apple [2] => orange )

In the above code, the $result array only contains elements that exist in $array1 but not in $array2, namely 'apple' and 'orange'.

To sum up, there are many ways to traverse arrays and compare values ​​in arrays in PHP. We can choose the appropriate way to process arrays according to the specific situation.

The above is the detailed content of How to traverse arrays and compare values ​​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