Comparison of key names in PHP array operations and methods for assigning values of difference sets and intersections _php tips

Source: Internet
Author: User

This article illustrates the method of comparing the key names of PHP array operations with the difference set and the intersection assignment. Share to everyone for your reference. The specific methods are as follows:

This example primarily implements the various common operations of an array. If you compare the difference set of the array to the key name, compute the difference set, insert an element into the specified array, reverse the array, and assign a new array to the intersection.

The specific code is as follows:

Copy Code code as follows:
Defining callback Functions
function Key_compare_func ($key 1, $key 2)
{
if ($key 1== $key 2)//If the two parameters are equal
return 0; return 0
else if ($key 1> $key 2)//if $key1> $key 2
return 1; Return 1
else//If $key1< $key 2
return-1; Return-1
}
Define two arrays individually
$array 1=array (' Blue ' =>1, ' Red ' =>2, ' green ' =>3, ' Purple ' =>4);
$array 2=array (' Green ' =>5, ' Blue ' =>6, ' yellow ' =>7, ' cyan ' =>8);
To compare the difference set of an array with a callback function on a key name
$result =var_dump (Array_diff_ukey ($array 1, $array 2, ' Key_compare_func '));
Print_r ($result);
Output comparison Results

$array 1=array ("A" => "green", "Red", "Blue", "Red"); Define Array 1
$array 2=array ("B" => "green", "yellow", "red"); Define Array 2
$result =array_diff ($array 1, $array 2); Calculation difference Set
Print_r ($result); Output comparison Results

//
$result =array_fill (5,6, ' banana '); Array $a add 6 banana starting from 5th element
Print_r ($result); Output results
//
function Odd ($var)
{
Return ($var%2==1); Returns true if the argument is odd, otherwise returns false
}
function even ($var)
{
Return ($var%2==0); Returns true if the argument is even, or false
}
Define two arrays individually
$array 1=array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
$array 2=array (6,7,8,9,10,11,12);
echo "Filter odd: N";
Print_r (Array_filter ($array 1, "odd")); Filter the odd number in array 1
echo "Filter even: n";
Print_r (Array_filter ($array 2, "even")); Filter the even number in array 2
//
$trans =array ("A" =>1, "B" =>2, "C" =>3); Defining arrays
$result =array_flip ($trans); Reverse Array
Print_r ($result); Output inverted Array
//
$array 1=array ("A" => "green", "B" => "Brown", "C" => "Blue", "Red");
$array 2=array ("A" => "green", "yellow", "red");
$result _array=array_intersect_assoc ($array 1, $array 2); Intersection assignment to $result_array
Print_r ($result _array); Array of output results

I hope this article will help you with your PHP program design.

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.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.