Stupid Bird Learn PHP (vi) array

Source: Internet
Author: User
Tags foreach array arrays key learn php return variable

An overview of arrays

1.1 Arrays are compound types

1.2 Arrays can store any length of data, or you can store any type of data


ii. type of array

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

 
  ';
	Print_r ($user);
	Echo '
';?>2.2 Associative array: Subscript is a string as an index

 
  ';
	Print_r ($user);
	Echo '
'; $user ["name"] = "Lisi"; echo $user ["name"];?>

three, multiple declarations of the array of ways

3.1 Assigning values directly to array elements

A. If the index subscript does not give a value, the index will start sequentially from 0
B. If an index subscript is given, the next one will be 1 from the maximum start

C. If the preceding subscript appears, if it is assigned, the previous element is assigned a value
D. Mixed declarations, indexes and associations do not affect each other (without affecting the declaration of index subscript)


3.2 using the array () function

A. The default is an indexed array

B. If you specify a subscript for an associative array and an indexed array, use the key => value
C. Use the "," split before multiple members

 
  ';
	Print_r ($user 1);
	Echo '
'; /** Array ([0] => 1 [1] => Zhsangsan [2] => [3] => nan [4] => aaa@bbb.com) */$user 2 = Array ("id" =>1 , "name" => "Zhsangsan", "Age" =>10, 100=> "Nan", "aaa@bbb.com"); Echo '
';
	Print_r ($user 2);
	Echo '
'; /** Array ([id] => 1 [name] => Zhsangsan [age] => [m] => nan [] => aaa@bbb.com) * * *?>

Important knowledge points: two-dimensional arrays

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

four, the array of traversal

4.1 Looping through using 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 be an associative array)

 
  ";
	}
	
	/**
	 	$user [0]=1
		$user [1]=zhasna
		$user [2]=aaa@bb.com
	 * *
?>

4.2 Loop traversal using a foreach statement (highly recommended)

The number of loops 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", "aaa@bb.com");
    foreach ($user as $key => $val) {   //$var is a custom variable, $key the custom variable
  		echo $key. "=====>". $val. "
"; } /* 0 =====> 1 name =====> Zhasna age =====> =====> nan- =====> aaa@bb.com / foreach ($user as $val) { //Not key can also directly traverse $user value echo $val. "
"; } / * 1 zhasna nan aaa@bb.com * * ?>

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

each () function:

* requires an array as an argument

* The return is also an array

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

* The default current element is the first element

* Move the current element backward after each execution

* If you have already done this function at the end, return false

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

List () function:

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

* The number of elements in the array is 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 value one by one in the array correspond to the echo $sex. "
"; If you do not want to assign a value to name, then write list (, $age, $sex)/ * Zansan nnnnn/ $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, "name" => "Zhangsan", "Age" =>10, "Sex" => "Nan"); while (the list ($key, $value) = each ($user 2)) { echo $key. "==>". $value. "
"; } /* name ==> Zhangsan age ==> sex ==> 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.