Php array operation's key name comparison and difference set, intersection assignment method, array intersection. Php array operation key name comparison and difference set, intersection assignment method, array intersection this article describes the php array operation key name comparison and difference set, intersection assignment method. The key name comparison and difference set and intersection assignment methods for php array operations, array intersection
This article describes how to compare and assign values between key names and difference sets and intersection values for php array operations. Share it with you for your reference. The specific method is as follows:
This instance mainly implements various common operations on arrays. For example, compare the key names to calculate the difference set of the array, calculate the difference set, insert an element to the specified array, and reverse the array and the intersection to assign a new array.
The code is as follows:
The code is as follows:
// Define the callback function
Function key_compare_func ($ key1, $ key2)
{
If ($ key1 = $ key2) // if the two parameters are equal
Return 0; // return 0
Else if ($ key1> $ key2) // if $ key1> $ key2
Return 1; // return 1
Else // if $ key1 <$ key2
Return-1; // return-1
}
// Define two arrays respectively
$ Array1 = array ('blue' => 1, 'Red' => 2, 'green' => 3, 'Purple '=> 4 );
$ Array2 = array ('green' => 5, 'blue' => 6, 'yellow' => 7, 'cya' => 8 );
// Use the callback function to compare the difference set of the calculated array with the key name
$ Result = var_dump (array_diff_ukey ($ array1, $ array2, 'Key _ compare_func '));
Print_r ($ result );
// Output the comparison result
$ Array1 = array ("a" => "green", "red", "blue", "red"); // defines array 1
$ Array2 = array ("B" => "green", "yellow", "red"); // defines array 2
$ Result = array_diff ($ array1, $ array2); // calculate the difference set
Print_r ($ result); // outputs the comparison result
//
$ Result = array_fill (5th, 'banana '); // add 6 banana entries from the first element of array $
Print_r ($ result); // output result
//
Function odd ($ var)
{
Return ($ var % 2 = 1); // if the parameter is odd, true is returned; otherwise, false is returned.
}
Function even ($ var)
{
Return ($ var % 2 = 0); // if the parameter is an even number, true is returned; otherwise, false is returned.
}
// Define two arrays respectively
$ Array1 = array ("a" => 1, "B" => 2, "c" => 3, "d" => 4, "e" => 5 );
$ Array2 = array (6, 7, 8, 9, 10, 11, 12 );
Echo "filter odd: n ";
Print_r (array_filter ($ array1, "odd"); // filter odd numbers in array 1
Echo "filter even numbers: n ";
Print_r (array_filter ($ array2, "even"); // filter the even numbers in array 2
//
$ Trans = array ("a" => 1, "B" => 2, "c" => 3); // define an array
$ Result = array_flip ($ trans); // reverse the array
Print_r ($ result); // Array after inverted output
//
$ Array1 = array ("a" => "green", "B" => "brown", "c" => "blue", "red ");
$ Array2 = array ("a" => "green", "yellow", "red ");
$ Result_array = array_intersect_assoc ($ array1, $ array2); // The intersection value is $ result_array.
Print_r ($ result_array); // output result array
I hope this article will help you with PHP programming.
Examples in this article describes how to compare and assign values between key names and difference sets and intersection values for php array operations. Share it with you...