Stupid Bird Learn PHP (6) array

Source: Internet
Author: User
Stupid Bird Learn PHP (vi) array

First, an array overview

1.1 Arrays are composite types

1.2 You can store any length of data in an array, or you can store any type of data


Ii. Types of arrays

2.1 Indexed array: Subscript is an ordinal integer as an index

 
  ';p rint_r ($user); Echo '
';? >

2.2 Associative arrays: subscript is a string as an index

 
  ';p rint_r ($user); Echo '
'; $user ["name"] = "Lisi"; Echo $user ["name"];? >

Three, the array of multiple declaration methods

3.1 Assigning values directly to an array element

A. If index subscript does not give a value, it will be indexed from 0
B. If the index subscript is given, the next one will be increased by 1 from the largest start

C. If the preceding subscript appears, if the assignment is to reassign the previous element
D. Mixed declarations, indexes and associations do not affect each other (do not affect the declaration of index subscript)


3.2 using the array () function

A. Default is an indexed array

B. If you specify subscripts for associative arrays and indexed arrays, use the key = = value
C. Multiple members before using "," split

 
  ';p Rint_r ($user 1); Echo '
';/** Array ([0] = 1 [1] = Zhsangsan [2] = [3] = nan [4] = [email protected]) */$user 2 = Array ("id "=>1," "Name" = "Zhsangsan", "Age" =>10, 100=> "Nan", "[email protected]"); Echo '
';p Rint_r ($user 2); Echo '
';/** Array ([id] = 1 [Name] = + Zhsangsan [age] [+] = [[101]] [email protected]] */?>

Important points of knowledge: two-dimensional arrays

 
  Array (Array (1, "Zansan", "Ten," "Nan"), Array (2, "Lisi", "NV")), echo $info ["User"][1][1]; Lisiecho '
';p rint_r ($info); Echo '
';/** array ([ user] = Array ([ 0] = = Array ( [0] = 1 [1] = = Zansan [2] = 10 [3] = nan ) [1] = = Array ( [0] = 2 [1] = Lisi [2] = [3] = NV )) */?>

Iv. Traversal of arrays

4.1 Loop traversal with a for statement (not recommended)

Limitations: The array must be an indexed array, and the subscript must be contiguous (however, the index array subscript can be discontinuous, and the array may also be an associative array)

 
  ";} /** $user [0]=1$user[1]=zhasna$user[2][email protected] */?>

4.2 Looping through using a foreach statement (highly recommended)

The number of cycles is determined by the number of elements in the array, and each loop assigns the elements in the array to the following variables

 
  "Zhasna", "Age" =>40, 100=> "Nan", "[email protected]");    foreach ($user as $key + $val) {   //$var is a custom variable $key a custom variable  echo $key. "=====>". $val. "
"; } /* 0 =====> 1name =====> zhasnaage =====> 40100 =====> nan101 =====> [email protected] */ Fore Ach ($user as $val) { //Do not key can also directly traverse the value of the $user echo $val. "
"; } /* 1 zhasna40nan[email protected] */?>

4.3 using while (), list (), each () combined loop traversal (not recommended)

each () function:

* requires an array as a parameter

* Returns an array as well

* Returns the array when 0, 1, key, value four subscript, 0 and key subscript are the keys of the current array element, 1 and value subscript are the values of the current array element

* The default current element is the first element

* The current element is moved backwards after each execution

* Returns False if the function is also executed at the end of the

 
  1, "name" = "Zhangsan", "Age" =>10, "Sex" and "Nan"), while ($arr = each ($user)) {//Echo $arr [0]. " ==> ". $arr [1]."
"Echo $arr [" Key "]."----> ". $arr [" Value "]."
";} /** ID----> 1name----> Zhangsanage----> 10sex----> Nan */?>

List () function:

* List () =array (); You need to assign an array to this function

* The number of elements in the array must be the same as the number of parameters in the list () function
* Each element value in the array is assigned to each parameter in the list () function, and list () converts each argument to a variable

* List () can only receive indexed arrays

 
  "Echo $age."
"; The values in the list and the values in the array one by one correspond to the echo $sex. "
"; If you do not want to assign a value to name, write it as list (, $age, $sex)/* zansan10nnnnn */$user 1 = array ("id" =>1, "name" = "Zhangsan", "Age" =>10, "Sex" = "nan"), List ($key, $value) = each ($user 1); Array ([1] = 1 [0] + ID ) echo $key. $value;/* ID-1 */$user 2 = array ("id" =>1, "nam E "=" Zhangsan "," Age "=>10," sex "=" Nan "), while (List ($key, $value) = each ($user 2)) {echo $key." ==> ". $value ."
";} /* Name ==> zhangsanage ==> 10sex ==> nan */?>







  • 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.