Copy codeThe Code is as follows:
<? Php
// Simulate SQL data
$ Array = array (0 => 'apple', 1 => 'bana', 2 => 'cat', 3 => 'Dog', 4 => 'egg ', '5' => 'father ');
// Function usage 1
// Arr is the input data. $ con is the condition.
Function f_1 ($ arr, $ con ){
// The array here is private in this function and will not conflict with the forward array
// Therefore, the array in the field is not used directly in the internal area, and the array in the external area cannot be used directly.
// First instance an array
$ Array = array ();
// For foreach while usage is similar, specific baidu
Foreach ($ arr as $ key => $ value ){
// If the cyclic value is equal to con, add it to the array.
If ($ value = $ con ){
// The difference between an array and a variable is that a [] is added.
$ Array [] = array ($ key => $ value );
}
}
// Returns an array after the result is obtained in a loop. Therefore, this function is an array.
Return $ array;
// After the return statement is executed, it will be disconnected and no code will be executed.
// Return can be seen as the end of a function.
}
// Function usage 2
// $ Con can be an array
Function f_2 ($ arr, $ con ){
// Instance a variable first
$ Code = '<ul> ';
Foreach ($ arr as $ key => $ value ){
// The for loop in It loops through con content
Foreach ($ con as $ value2 ){
//. = Add more consecutive variable definitions later
// If the value generated by the first data loop is the same as that generated by the second conditional loop, add it to the variable.
// Multiple for loops are used to filter data, also known as recursion.
If ($ value = $ value2 ){
$ Code. = '<li>'. $ value. '</li> ';
}
}
}
$ Code. = '</ul> ';
// Return the variable after the result is obtained in a loop. Therefore, this function is a string.
Return $ code;
}
// Function usage 3
// The difference between echo and return in the function depends on the execution result.
Function f_3 ($ arr, $ con ){
// Instance a variable first
Echo '<ul> ';
Foreach ($ arr as $ key => $ value ){
// The for loop in It loops through con content
Foreach ($ con as $ value2 ){
//. = Add more consecutive variable definitions later
// If the value generated by the first data loop is the same as that generated by the second conditional loop, add it to the variable.
// Multiple for loops to filter data are also called Recursion
If ($ value = $ value2 ){
Echo '<li>'. $ value. '</li> ';
}
}
}
Echo '</ul> ';
}
?>
F_1 output start <br/>
<? Php
// Because f_1 is an array, We can print it out.
Print_r (f_1 ($ array, 'Banana '));
?>
<Br/> f_1 output end
<Hr/> <br/>
F_2 output start <br/>
<? Php
// F_2 is a variable
$ Con = array ('apple', 'Father ');
Echo f_2 ($ array, $ con );
?>
<Br/> f_2 output end
<Hr/> <br/>
F_2 output start <br/>
<? Php
// F_3 has already been echo in the function, so no echo is required during function execution.
$ Con = array ('apple', 'Father ');
F_3 ($ array, $ con );
?>
<Br/> f_2 output end