PHP array application basics (5)

Source: Internet
Author: User
Abstract: in_array () checks whether a value exists in the array. array_search () searches for a given value in the array. if the value is successful, the corresponding key name, array_key_exists () is returned () check whether the given key name exists in the array... 1. the foreach loop statement traverses two-dimensional arrays. This is the foreach traversal of a two-dimensional array.
$ Erwei_arr = array (
Array ("xiaoliu", "123456", "Liu", "male", 29, "system analyst "),
Array ("xiaozeng", "123456", "Xiao Zeng", "male", 23, "Web engineer "),
Array ("xiaochen", "123456", "Xiao Chen", "male", 29, "Java engineer ")
); // Declare an indexed two-dimensional array

$ Html ="

"; // Defines the string $ html. the content of the string is in the table.








TagForeach ($ erwei_arr as $ field ){$ Html. =" "; // Use the string operator to compare the original value of $ html" Connect to a new string, the same belowForeach ($ field as $ value ){$ Html. =" ";}$ Html. =" ";}$ Html. ="
". $ Value ."
";

Echo $ html;

The result is

This is how PHP operates tables. Haha.

2. Common Array processing functions

Array processing functions are also important, such as random functions and sorting functions. There are roughly:

Index/value operation function

In_array () check whether a value exists in the array. array_search () searches for the given value in the array. if the value is successful, the corresponding key name, array_key_exists () is returned () check whether the given key name exists in the array

Array sorting function

Sort () is used to sort the array values in ascending order and reenumerate the array. rsort () is in descending order, which is the opposite. Asort () sorts the array values in ascending order to maintain the association between indexes and values. arsort () is the opposite. Ksort () is used to sort the array by key name in ascending order to maintain the association between the index and the value. krsort () is the opposite.

Array statistics/unique functions

As mentioned earlier, the count () function is used to calculate the number of array elements in an array.

Split, merge, and split array functions

Array_combine () and array_merge ().
$ One_array = array ("name", "gender", "age", "title ");
$ Two_array = array ("Xiaozeng", "male", 23, "e-commerce instructor ");

Echo"

**********

";
$ Nc_array = array_combine ($ one_array, $ two_array );
Print_r ($ nc_array );

The result is Array ([name] => Xiao Zeng [gender] => male [age] => 23 [title] => e-commerce instructor)

Echo"

**********

";
$ Nm_array = array_merge ($ one_array, $ two_array );
Print_r ($ nm_array );

The result is Array ([0] => name [1] => gender [2] => age [3] => title [4] => Xiao Zeng [5] => male [6] => 23 [7] => e-commerce instructor)
Random functions

This should be very common. some random articles, random advertisements, or lucky draws on websites use this principle in PHP.

Main functions include array_rand () and shuffle ().

Array_rand () is to randomly retrieve or specify an array element from the array, and return an array containing a random key name.

Shuffle () is to randomly disrupt an array.

Here we are. Pig's blog is busy recently, so the update is unstable. I hereby declare that part of the code refers to the source code of my instructor Zeng Wenbing.

The above is the basic content of PHP's array application (5). For more information, see PHP's Chinese website (www.php1.cn )!

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.