Php array operation key name comparison difference set intersection value

Source: Internet
Author: User

// 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

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.