The example in this article describes the methods of key name comparison, difference set, and intersection assignment in PHP array operations. Share it with everyone for your reference. The specific method is as follows:
This example mainly implements various common operations on arrays. For example, comparing key names and calculating the difference set of arrays, calculating the difference set, inserting an element into the specified array, inverting the array and assigning a new array at the intersection, etc.
The specific code is as follows:
//
$result=array_fill(5,6,'banana'); //Add 6 bananas starting from the 5th element to array $a
print_r($result); //Output result
//
function odd($var)
{
Return($var%2==1); //If the parameter is an odd number, return true, otherwise return false
}
function even($var)
{
Return($var%2==0); //If the parameter is an even number, return true, otherwise return false
}
//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 numbers: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 even numbers in array 2
//
$trans=array("a"=>1,"b"=>2,"c"=>3); //Define array
$result=array_flip($trans); //Reverse array
print_r($result); //Output the reversed array
//
$array1=array("a"=>"green","b"=>"brown","c"=>"blue","red");
$array2=array("a"=>"green","yellow","red");
$result_array=array_intersect_assoc($array1,$array2); //Assign intersection to $result_array
print_r($result_array); //Output result array
I hope this article will be helpful to everyone’s PHP programming design.