PHP Array operation key name comparison with difference set, intersection assignment method, array intersection _php tutorial

Source: Internet
Author: User

PHP Array operation key name comparison and difference set, intersection assignment method, array intersection


In this paper, we describe the methods of the comparison of key names of PHP array operations with the difference set and intersection assignment. Share to everyone for your reference. Here's how:

This example mainly implements a variety of common operations of the logarithmic group. For example, compare the difference of the array to the key name, calculate the difference set, insert an element into the specified array, invert the array with the intersection assignment new array, etc.

The specific code is as follows:

The

copy Code code is as follows://define callback function
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 respectively
$array 1=array (' Blue ' =>1, ' Red ' =>2, ' green ' =>3, ' Purple ' =>4 );
$array 2=array (' Green ' =>5, ' Blue ' =>6, ' yellow ' =>7, ' cyan ' =>8);
//Use a callback function to compare the difference set of an array with a key name
$result =var_dump (Array_diff_ukey ($array 1, $array 2, ' Key_compare_func '));
Print_r ($result);
//Output comparison result

$array 1=array ("a" = "green", "Red", "Blue", "red");//define array 1
$array 2=array ("b" = "green" , "Yellow", "red"); Defines an array of 2
$result =array_diff ($array 1, $array 2);//Differential set
Print_r ($result);//output comparison result

//
$result =array_fill (5,6, ' banana '); An array of $ A adds 6 banana starting with the 5th element
Print_r ($result); Output results
//
function Odd ($var)
{
Return ($var%2==1); Returns true if the argument is odd, otherwise false
}
function even ($var)
{
Return ($var%2==0); Returns true if the argument is even, false otherwise
}
Define two arrays separately
$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); Invert an 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 is helpful to everyone's PHP programming.

http://www.bkjia.com/PHPjc/909344.html www.bkjia.com true http://www.bkjia.com/PHPjc/909344.html techarticle 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. To share with you ...

  • 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.