Php array operation example 3 _ PHP Tutorial

Source: Internet
Author: User
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...

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.