For you to introduce several PHP output array of the same name elements of the method, the need for friends, you can refer to the next. Method 1, directly with PHP built-in function array_intersect () array array_intersect (array $array 1, array $array 2 [, array $ ...]) Array_intersect () Returns an array that contains all the values in Array1 that also appear in all other parameter arrays. Note The key name remains unchanged. Example:
"Green", "Red", "Blue"); $array 2 = Array ("b" = "green", "yellow", "red"); $result = Array_intersect ($array 1, $array 2); ? >
Output: Array ([a] = green [0] = + red) Method 2, self-implemented algorithm
"; $k =var_dump (Array_unique ($r)); return $k; } $a = Array ("Red", "green", "pink", "red", "yellow", "pink", "red"); $r =my_array_same ($a); Var_dump (Array_unique ($r)); ? >
Output: Array (2) {[3]=> string (3) "Red" [5]=> string (4) "Pink"} 3. Custom Recursive functions
"; Print_r (My_array_intersect ($array 1, $array 2)); echo ""; ? >
Description: In one-dimensional arrays, the third algorithm is faster than the first type. All of these algorithms apply to one-dimensional arrays, so how do multidimensional arrays find the same elements? Here is a way of thinking: You can convert multidimensional arrays into one-dimensional arrays, and then use the above algorithm output. Example:
$v) { if (!is_array ($v)) { $str. = $v. " "; } else{ $str. =toarr ($v); } } return $str; } /* Recursive function End */ ?>
The above code implements a multidimensional array into a string, which is then converted into a one-dimensional array using the Expode function. Imagine that a database returns a value with the same name as a field, which is also possible with SQL statements. Programming is like this, all the way to Rome, a return to three, touch and bypass, more learning more practice, charm ah. The programmer's home, wish everybody to study the progress. |