This article mainly focuses on examples such as array intersection, determining whether a key exists in an array, merging two arrays, sorting data, adding arrays, deleting array elements, and randomly extracting array elements.
This article mainly focuses on examples such as array intersection, determining whether a key exists in an array, merging two arrays, data sorting, adding arrays, deleting array elements, randomly extracting array elements, etc.
*/
$array1=array("a"=>"green","red","blue");
$array2=array("b"=>"green","yellow","red");
$result=array_intersect($array1,$array2);//Calculate intersection and assign value
print_r($result);
//
$search_array=array('first'=>1,'second'=>4); //Define array
if(array_key_exists('first',$search_array)) //Determine whether the key exists in the array
{
echo "the 'first' element is in the array"; //Output the corresponding information
}
//
$ar1=array("color"=>array("favorite"=>"red"),5);
$ar2=array(10,"color"=>array("favorite"=>"green","blue"));
$result=array_merge_recursive($ar1,$ar2); //Merge two arrays and return a result array
print_r($result); //Output result
//
$array1=array("color"=>"red",2,4);
$array2=array("a","b","color"=>"green","shape"=>"trapezoid",4);
$result=array_merge($array1,$array2); //Merge two arrays
print_r($result); //Output result content
//
$ar1=array("10",100,100,"a"); //Define array 1
$ar2=array(1,3,"2",1); //Define array 2
array_multisort($ar1,$ar2); //Sort 2 arrays
var_dump($ar1); //Print the sorted results
var_dump($ar2); //Print the sorted results
//
$stack=array("orange","banana","apple","rasp tutorialberry"); //Define an array
$fruit=array_pop($stack); //The last element pops from the stack
print_r($stack); //Display the results
//
$stack=array("orange", "banana"); //Define the original array
array_push($stack,"apple","raspberry"); //Perform stack push operation
print_r($stack); //Display the result, four values will be returned
//
srand((float)microtime()*10000000); //Seed the random number generator
$input=array("neo","morpheus","trinity","cypher","tank"); //Define the original array
$rand_keys=array_rand($input,2); //Randomly select array elements
print $input[$rand_keys[0]]."n"; //Display randomly selected array elements respectively
print $input[$rand_keys[1]]."n";