PHP Arrays Array

Source: Internet
Author: User

array of direct assignment declarations

1. An array contains multiple contents, and the contents of an array are called "elements".

2. Each element is a value that consists of a key and a value of Key/value,key subscript value

$arr []=0; $arr [2]=2; $arr ['three']=3;

3. Use the value by key

There are two types of subscripts: One is an integer (an indexed array) and one is a string (associative array)

4.[] to operate the subscript, you can also use {} to Exchange, it is recommended to use [] to specify the subscript

5. When using subscript, if it is an associative array, be sure to use quotation marks instead of the constant name.

6. Array of auto-growth subscript, the default is starting from 0, auto-growth is the maximum value plus 1

7. String subscript for associative arrays, without affecting the arrangement rules for all subscripts!

Declare an array using the array () function, which is the index's subscript by default, starting at 0

8. Specify subscript with the/= symbol

After 9.php5.4, you can declare an array like any other language.

$arr =['aaa','bbb',3='Hello  ','cccccccc'];

10. Deleting an element in an array, the unset () function allows a key in the array to be deleted.

Note, however, that the array will not rebuild the index, and you can use the Array_values () function if you need to rebuild the index after deletion.

Application Example: Monkey Choose King

<?PHPfunctionSelectking ($m,$n){        $arr=Array(); $a= ' A ';  for($i= 0;$i<$m;$i++){            $arr[] =$a++; }        $j= 0;  while(Count($arr) >1){            if($j%$n==0){                unset($arr[$j]); }Else{                $arr[] =$arr[$j]; unset($arr[$j]); }            $j++; }        return $arr; }    Var_dump(Selectking (30,3));?>
View Code PHP Two-bit arrays and multidimensional arrays

Traversal of an array

I. Using the FOR statement to iterate through an array (guaranteed array, must be subscript consecutive index array)

Insufficient:
1. Array subscripts are not necessarily contiguous

2. Associative array for cannot traverse out value

Two. Iterating through an array using a foreach statement

foreach  as Custom variables) {    ... }

1. How many elements are in the array, and how many times the foreach loops

2.foreach will assign the elements in the array to the custom variables in each loop, in turn,

This variable is used in each loop to use the elements in the current array.

foreach  as subscript variable = = value variable) {      if(is_array())        ... }

Three. Combine with list (), each () and while loop to iterate through an array

List () function but the use of this function differs from other functions

Function: Converts an element in an array to a variable use

1. Use the list () function to the left of the equals sign and only an array to the right of the equals sign

2. There are several elements in the array, there are several parameters in the list (), and the parameter must be a variable (the newly declared custom variable), not a value

List ($a,$b,$carray(' Zhangsan ', ' Lisi ', ' Wangwu ');

3. The index array (subscript is continuous) can only be converted to a variable, which is started by subscript 0.

4. The element in the array can be selectively received in the list () parameter by a null item

List ($a,,$carray(' Zhangsan ', ' Lisi ', ' Wangwu ');

Each () is just a function, the parameter is an array as an argument, the returned value is also an array

1. The returned value is an array, the array is fixed with 4 elements, and the subscript is fixed, 1 (value) value (value) 0 (subscript) key (subscript)

2.each () only handles the current element (the default current element is the first element, the pointer points to the first element), the current element is transformed into an array of information, and after processing, the pointer moves down one element

3. If the pointer is already in the end position, then use each () to get the element, return False

$arr Array (' Zhangsan ', ' Lisi ', ' Wangwu ');  while (list($key,$valeach ($arr)) {        echo$ Key. ', '$val. ' <br/> ';}

Iterating through an array using the Inner pointer control function of an array

Next (), Prev (), End () and reset ()

These four functions control the position of the pointer in the current array. Next () is responsible for moving the pointer backwards, prev () is responsible for moving the pointer forward; end () points the pointer to the last element in the array, and the reset () function moves the current pointer unconditionally to the first index position. The parameters of these functions are arrays.

Key () Gets the key name of the current array, and current () Gets the value key values for this array.

Hyper-Global Array

Super Global Array (variable), in PHP script, already declared the variables, you can go directly to use it! The name of the variable is already set.

$_server

$_env

$_get

$_post

$_request

$_files

$_cookie

$_session

$_globals

1. Arrays (associative arrays), just as we do with our own declared arrays

2. Global

3. Each predefined array has its own unique function

PHP Arrays Array

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.