PHP provides us with a variety of system functions to implement the various functions we need, so how do we implement these functions without using system functions? Here is how several system functions are implemented.
First, let's define an array:
$arr = Array (1,2,3,4,5,6, "a" =>7, "B" =>8, "c" = "haha", 10);
Here are the system functions that can be used for this array and how to get the same effect without using system functions:
1. Returns all the values of the array, returning the array. (Array_values ($arr))
This function is very simple to implement, just need to use the value of the function as a foreach to assign to the new array. Here is the implementation code:
function GetValue ($arr) { $arr 1 = array (); foreach ($arr as $key = + $value) { $arr 1[]= $value; } return $arr 1;}
GetValue ($arr);
In this way, we take out all the values of the $arr .
2. Returns all the keys of the array, returning the array. (Array_keys ($arr))
This function and the previous function are basically the same way, just need to change the value assigned to $ARR1 to $arr key. The code is as follows:
function GetKey ($arr) { $arr 1 = array (); foreach ($arr as $key = + $value) { $arr 1[]= $key; } return $arr 1;} GetKey ($arr);
This allows you to remove all the keys from the array.
3. Detects if the array contains a value, returns TRUE or false, and evaluates to the condition that ===,false is = =. (In_array ("8", $arr, True))
This function is also very simple, the parameters are passed to the function, respectively, and each of the values in the group comparison, found that the same words return true, and finally did not find the return false. Here's the code:
function test ($num, $arr, $isTrue =true) {
foreach ($ Arr as $key = $value) {
if ($isTrue) {
if ($num = = = $value) {
return true;
}
}else{
if ($num = = $value) {
return true;
}
}
}
return false;
}
This function requires a third parameter to determine whether the detection condition is = = or = = =, this practice will $isTrue by default set to true that if you do not pass in the third parameter, the default criterion is = = =.
4. Swap the keys and values in the array to return the new array after the interchange. (Array_flip ($arr))
This function is implemented in the same way as the first two, using the Foreach loop to set the array's key as the value of the new array, and the value of the array as the key for the new array. The code is as follows:
function Change ($arr) { $arr 1 = array (); foreach ($arr as $key = $value) { $arr 1[$value]= $key; } return $arr 1;} Change ($arr);
This function only needs to $arr 1[$value]= $key; Key-value exchange can be achieved.
5. Count the number of array elements. (count ($arr))
The code is as follows:
function num ($arr) { $i =0; foreach ($arr as $key = + $value) { $i + +; } return $i;} Num ($arr);
6. Count the number of occurrences of all the values in the array and return a new array. (Array_count_values ($arr))
New array format: The value of the original array (the value after the reset)
Value--The number of occurrences of the corresponding value in the original array.
function Check ($arr) { $arr 1=array (); foreach ($arr as $key = + $value) { $isHas =true; foreach ($arr 1 as $key 1=> $value 1) { if ($key 1== $value) { $arr 1[$value]++; $isHas =false; } } if ($isHas) { $arr 1[$value]=1; } } return $arr 1;} Check ($arr);
Implementation ideas:
1. There is an empty array arr1: Key----the value of the original array de-weight and the number of occurrences of each value of the original array
2. Traverse the original array arr, and remove each value in arr;
3. Detection: Whether the newly fetched value has a key with the same name in ARR1.
If there is a value that was found to be duplicated with the new value now, then the ARR1
The value of the key corresponds to +1;
If not, the description has not been duplicated with the newly fetched value until now, so the arr1
Creates a new key with the same name, with a value of 1;
7. Move the duplicate values in the divisor group. (Array_unique ($arr))
The simple implementation of this function is to exchange the key value two times, the duplicate value can be used with the same name as the value of the key will be replaced by the principle of elimination, but this method can only be used in the index array, used to associate the array when the key is also deleted the bug. The code is as follows:
function Delete ($arr) { $a =array (); $b =array (); foreach ($arr as $key = + $value) { $a [$value]= $key; } foreach ($a as $key 1=> $value 1) { $b [$value 1]= $key 1; } return $b;} Delete ($arr);
8. Filter each value in the array:
① does not pass the callback function: filter out all null values (0/""/null/false/"0"/[])
② callback function: You need to pass a parameter to the callback function to determine if the parameter meets the requirements, and if so, return true; otherwise, return false;
The code is as follows:
function Filterarray ($arr, $func =false) { $NEWARR = array (); if ($func ==false) { echo] does not have a callback function, the null value should be removed! "; foreach ($arr as $key = + $value) { if ($value) { $NEWARR [$key] = $value;}} } Else{ echo "has a callback function that should be filtered by the callback function!" "; foreach ($arr as $key = + $value) { if ($func ($value)) { $NEWARR [$key] = $value;}} } return $NEWARR;} Filterarray ($arr, function ($value) { if ($value >5) { return true; } else{ return false; }}) ;
The second parameter of the function can determine if a callback function is passed in, does not perform the outermost if, and some of the outermost else is executed, because null values are false in the if judgment condition and can be skipped when assigning a value to a new array.
The outer outermost else if is judged by the value returned by the callback function to be true or false so that it can be filtered with its own set of conditions.
9. Sort the array (ascending).
This function uses the idea of bubble sort, this function only need to row number, therefore, redefine an array
$arr =[1,7,140,3,10,12,134,239,453,24,56,34,78,53,56];
The code is as follows:
function Paixu ($arr) { $len = count ($arr); for ($x =0; $x< $len-1; $x + +) { for ($y=0; $y < $len-$x-1; $y + +) { if ($arr [$y]>$arr [$y +1]) { $old = $arr [$y]; $arr [$y]= $arr [$y +1]; $arr [$y +1]= $old; } }} return $arr;} Paixu ($arr);
The inner Loop realizes the public energy:
22 pair, big back move, small forward (i.e. if $arr[y]> $arr [y+1], $arr [y] and $arr[y+1] Swap position, you can move the largest to the last, where the $y< $len-$ X-1 is because the second loop does not need to verify the last one,
The third cycle does not need to validate the last two bits, and so on.
The above is the way to implement the function of system function without system function.
How to implement the functions of array system functions in PHP without using system functions