Introduction: If you are familiar with array-related knowledge in PHP, you will have a deep understanding of PHP and will handle many things. The PHP array is indeed very powerful, every PHP developer must be familiar with it so that it can be used freely in the project. 1. The array_values () function returns an array containing all the keys in the given array without retaining the key name.
Introduction: If you are familiar with array-related knowledge in PHP, you will have a deep understanding of PHP and will handle many things. The PHP array is indeed very powerful, every PHP developer must be familiar with it so that it can be used freely in the project. 1. The array_values () function returns an array containing all the keys in the given array without retaining the key name.
Lead:
If you are familiar with array-related knowledge in PHP, you will have a deep understanding of PHP and will handle many things. The PHP array is indeed very powerful, every PHP developer must be familiar with it so that it can be used freely in the project.
I. array_values ()
This function returns an array containing all the key values in the given array without retaining the key name.
For example, $ va = array (1 => 23, 'L' => 45, 9 => 90); array_values ($ va); the array (, 90) is obtained );
This function uses a lot of functions. For example, after obtaining the list content in the database, foreach analyzes the data. If each record does not meet the relevant conditions, it will filter out, in this case, the key value of the array will change. In this case, the array_values function can be used:
$ Result = M ("ly_vip")-> select ();
Foreach ($ result as $ k => $ v ){
If (! $ V ['isonly']) continue;
$ Res [$ k] = $ v;
}
$ Res = array_values ($ res );
Ii. array_diff ()
Returns an array containing all values in the array of array1 but not in any other parameter array. Note that the key name remains unchanged.
For example, $ arr1 = array (1, 2, 3, 4 );
$ Arr2 = array (2, 3, 9, 10 );
$ Diff = array_diff ($ arr1, $ arr2 );
The value of $ diff is array );
Application Scenario: This method was used in a development project some time ago and it feels good. Let's share it with you:
There are more than 3000 details pages in the project. You can use scheduled tasks to generate static pages in the early hours of every day. This involves the possible expiration of details pages, the redundant data of yesterday will be deleted. Otherwise, the user can access non-existent static pages, which will be very troublesome. In this case, the array_diff function can be used, he can better handle this problem. We can record the id of the static page generated every day to an array to keep the file, after the static page is produced every day, compare it with the array id of yesterday, for example, yesterday's $ tmp2 = array (); today's is $ tmp2 = array () you can use the following method to delete unnecessary data from yesterday:
$ Diff_tmp = array_diff ($ tmp2, $ tmp1 );
Foreach ($ diff_tmp as $ k => $ v ){
@ Unlink ("./play/". $ v. ". shtml.
}
Iii. array_map ()
The callback function is used in a given callback function. This callback function is used in each cell in the array and returns an array. The key value remains unchanged.
Returns an array containingArr1All units inCallbackThe Unit after the function.CallbackThe number of accepted parameters should be passedArray_map ()The number of arrays of the function is the same.
1. Let's first look at the use of array_map () in the function, as shown below
$arr = array('s'=>1,'sa'=>2,'tm'=>array(1=>2,4=>9));function getArr($value){return is_array($value)?array_map('getArr',$value):$value+1;}$smm = getArr($arr);print_r($smm);
Printed result
First, analyze the getArr function. This function is used to apply the value of each unit to each unit in the array, and then return the value. The key value remains unchanged, you can also see the printed result. Each unit element + 1 returns
Application scenarios:
For example, if we want to obtain the data in the database, we need to process each item. For example, if we want to reverse the meaning of each unit in each record, we can use the array_map function at this time, the following code is used:
Function Section
function stripslashes_deep($value){$value = is_array($value) ? array_map('stripslashes_deep', $value) : stripslashes($value); return $value;}
Class in Controller
public function getFilm(){ $film = M("Film"); $data = array('status'=>1,'isonly'=>2); $result = $film->where($data)->select(); if($result){ foreach($result as $k=>$v){ $result[$k] = stripslashes_deep($v); } }else{ return FALSE; } return $result;}
The preceding example shows an application scenario, which is similar to other scenarios.
2. How to Use
After reading the column in column 1 above, a friend may ask, what should I do if I use the array_map function to call back the internal method in the PHP class ?, Then there is no need to write the stripslashes_deep function to a function file. Can it be written to the class as a method in the class? Let's look at the following classes:
class A{ function sn($n){ return $n*$n*$n; } function test(){ $arr = array(1,2,4); $array = array_map(array($this,"sn"), $arr); print_r($array); }}$aa = new A();$aa->test();
Check the output result:
The above are two ways to use the array_map function. Thank you for your comments!
Iii. array_filter () function
Use the callback function to filter the elements in the array. If there is no callback function, the default value is to delete the content with the value of false in the array.
Instructions for use;
ArrayArray_filter(Array$ Input[, Callback$ Callback])
Instance 1:
$array = array(1=>0,2=>34,9=>223,'s'=>null,'sg'=>'');var_dump(array_filter($array));
Print results
Instance 2: With callback function
Function checkId ($ id ){
Return ($ id % 2 = 1)
}
$ Ids = array (1, 2, 4, 5, 7, 8, 9 );
Array_filter ($ ids, 'checkid ');
The returned result is array );
Note: What is the difference between array_filter and array_map?
A. When array_map is used as the callback function, the parameter locations transmitted by the function are different. array_map first passes the callback function and then transmits data, while array_filter calls back the function after the data is first transmitted.
B. After the callback function is used by array_map, the number of new arrays returned remains unchanged, while that of array_filter is not!
Other functions are added one after another. Please pay attention to PHP Array Processing (2 )...................