Get the App
SLTechnology News&Howtos  ›  Development  › 

Comparison of key names and assignment methods of subtraction and intersection in php Array Operation

Shulou Source: shulou.com Published: 2022-06-03 19:50:06 09月29日 Update

This article mainly introduces "the method of key name comparison and difference and intersection assignment in php array operation". In the daily operation, it is believed that many people have doubts about the method of key name comparison, difference and intersection assignment in php array operation. The editor consulted all kinds of data and sorted out simple and useful operation methods. I hope it will be helpful for you to answer the doubts of "key name comparison and subtraction, intersection assignment method in php array operation"! Next, please follow the editor to study!

This example mainly implements a variety of common operations on arrays. Such as comparing the key name to calculate the difference of the array, calculating the difference, inserting an element into the specified array, reversing the array and the intersection to assign a new array, and so on.

The specific code is as follows:

The copy code is as follows:

/ / define callback function

Function key_compare_func ($key1,$key2)

{

If ($key1==$key2) / / if the two parameters are equal

Return 0; / / returns 0

Else if ($key1 > $key2) / / if $key1 > $key2

Return 1; / / returns 1

Else / / if $key11,'red'= > 2 minus greenlies = > 3 memes purposely = > 4)

$array2=array ('green'= > 5 menstrual blueprints = > 6 recordings yellowship = > 7 recordings = > 8)

/ / calculate the difference of the array by comparing the key names with the callback function

$result=var_dump (array_diff_ukey ($array1,$array2,'key_compare_func'))

Print_r ($result)

/ / output comparison results

$array1=array ("a" = > "green", "red", "blue", "red"); / / defines the array 1

$array2=array ("b" = > "green", "yellow", "red"); / / define array 2

$result=array_diff ($array1,$array2); / / calculate the difference

Print_r ($result); / / output comparison results

/ /

/ / add 6 banana to the array $a starting with the fifth element

Print_r ($result); / / output the result

/ /

Function odd ($var)

{

Return ($var%2==1); / / returns true if the parameter is odd, false otherwise

}

Function even ($var)

{

Return ($var%2==0); / / return true if the parameter is even, false otherwise

}

/ / 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 number: n"

Print_r (array_filter ($array2, "even")); / / filter even numbers in array 2

/ /

$trans=array ("a" = > 1, "b" = > 2, "c" = > 3); / / define the array

$result=array_flip ($trans); / / reverse the array

Print_r ($result); / / output inverted array

/ /

$array1=array ("a" = > "green", "b" = > "brown", "c" = > "blue", "red")

$array2=array ("a" = > "green", "yellow", "red")

$result_array=array_intersect_assoc ($array1,$array2); / / intersection assignment to $result_array

Print_r ($result_array); / / output result array

At this point, on the "php array operation of key name comparison and difference, intersection assignment method" is over, I hope to be able to solve everyone's doubts. The collocation of theory and practice can better help you learn, go and try it! If you want to continue to learn more related knowledge, please continue to follow the website, the editor will continue to work hard to bring you more practical articles!

Tags: Array difference intersection method output result learning code even parameter odd two element function more help practical and then examples common Apple Docker Huawei Linux macOS MariaDB Microsoft MySQL NVidia OPPO Reno vpn Shulou Technology OPPO Reno Xiaomi Microsoft