Php array operation Example 3. This article mainly targets array intersection, whether the judgment key exists in the array, merges two arrays, sorts data, adds an array, deletes array elements, and randomly extracts array elements. This article mainly targets array intersection, whether the judgment key exists in the array, merges two arrays, sorts data, adds an array, deletes array elements, and randomly extracts array elements.
This article mainly targets array intersection, whether the judgment key exists in the array, merges two arrays, sorts data, adds an array, deletes array elements, and randomly extracts array elements.
*/
$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_intersect ($ array1, $ array2); // calculates the intersection and assigns a value
Print_r ($ result );
//
$ Search_array = array ('first' => 1, 'second' => 4); // define an array
If (array_key_exists ('first', $ search_array) // checks 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); // combine two arrays to 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"); // defines array 1
$ Ar2 = array (1, 3, "2", 1); // defines array 2
Array_multisort ($ ar1, $ ar2); // sort two arrays
Var_dump ($ ar1); // Print the sorted result
Var_dump ($ ar2); // Print the sorted result
//
$ Stack = array ("orange", "banana", "apple", "rasp tutorial berry"); // defines an array
$ Fruit = array_pop ($ stack); // output stack of the last element
Print_r ($ stack); // display the result
//
$ Stack = array ("orange", "banana"); // defines the original array
Array_push ($ stack, "apple", "raspberry"); // execute the stack operation
Print_r ($ stack); // display the result. four values are returned.
//
Srand (float) microtime () * 10000000); // seeding for the random number generator
$ Input = array ("neo", "morpheus", "trinity", "cypher", "tank"); // define the original array
$ Rand_keys = array_rand ($ input, 2); // randomly selects array elements.
Print $ input [$ rand_keys [0]. "n"; // displays randomly selected array elements.
Print $ input [$ rand_keys [1]. "n ";
Bytes. This...