Home >Backend Development >PHP Problem >How to exclude certain values from an array in php
In the PHP programming language, array is a very commonly used data type. When we need to find or operate elements in an array, sometimes we need to exclude certain specified values. In this article we will learn how to exclude certain values from an array in PHP.
1. Use the array_diff() function
The array_diff() function in PHP can be used to compare the difference between two or more arrays and return the difference result. We can use this function to exclude certain specified values from the array. The way to use this function is as follows:
// 定义一个数组 $array1 = array(1,2,3,4,5); // 定义一个需要排除的数组 $array2 = array(3,4); // 利用array_diff()函数排除数组中的值 $output = array_diff($array1,$array2); // 输出结果 print_r($output);
The above code will output the new array obtained after excluding the elements in the $array2 array. The output result is as follows:
Array ( [0] => 1 [1] => 2 [4] => 5 )
As shown above, we have successfully excluded the array There are two values 3 and 4 in .
2. Use the array_filter() function
The array_filter() function in PHP can be used to filter the values in the array. We can use this function to exclude certain specified values from the array. The way to use this function is as follows:
// 定义一个数组 $array = array(1,2,3,4,5); // 定义需要排除的值 $exclude_array = array(3,4); // 利用array_filter()函数过滤数组中的值 $output = array_filter($array,function($value) use ($exclude_array){ // 过滤需要排除的值 if(in_array($value,$exclude_array)){ return false; }else{ return true; } }); // 输出结果 print_r($output);
In the above code, we implement the array filtering function through an anonymous function. The function of this function is to return whether the currently traversed element needs to be filtered. Returns false if the element needs to be excluded, true otherwise. Use the in_array() function to determine whether the current element is in the array that needs to be excluded. The output is as follows:
Array ( [0] => 1 [1] => 2 [4] => 5 )
As shown above, we successfully excluded the two values 3 and 4 in the array.
3. Use foreach() loop
We can also use PHP's foreach() loop to traverse the array and exclude specified values based on certain conditions. The specific operation is as follows:
// 定义一个数组 $array = array(1,2,3,4,5); // 定义需要排除的值 $exclude_array = array(3,4); // 遍历数组并排除需要排除的元素 $output = array(); foreach($array as $value){ if(!in_array($value,$exclude_array)){ $output[] = $value; } } // 输出结果 print_r($output);
In the above code, we loop through the array through foreach() to determine whether the current element needs to be excluded. If exclusion is required, the element will not be added to the result array, otherwise the element will be added to the result array. The output result is as follows:
Array ( [0] => 1 [1] => 2 [4] => 5 )
As shown above, we have also implemented the operation of excluding specified elements in the array.
Summary:
The above three methods can be used to exclude certain specified values in PHP arrays. Using the array_diff() function and array_filter() function is relatively simple and requires less code, but performance may be affected to a certain extent. Although the code for using the foreach() loop is relatively large, it is more flexible and can be flexibly applied according to specific application scenarios. In actual development, we can choose the appropriate method to perform array operations based on the actual situation.
The above is the detailed content of How to exclude certain values from an array in php. For more information, please follow other related articles on the PHP Chinese website!