PHP Collation (ii): arrays

Source: Internet
Author: User
Tags prev type null

Array:

First of all, the understanding of PHP, to build a good understanding of the model is still critical:

The array in 1.PHP can actually be understood as the key value pair,key=>value; and the value of the key, can be String/integer;value is any data type, not only the basic data type, reference type is also possible, such as arrays;

2. Since key is a data type, there is a mutual conversion, when the definition of key is 1.0, it will be converted to 1, and so on, of course, key can also be empty, can also be repeated, but the back will overwrite the previous

3. Because it is a weak language, there are a lot of loose appearance, such as arr[]= "value", which can also exist, will directly create an array (understand this, and then the precise understanding and collation);

Definition: The array in PHP is actually an ordered map. A mapping is a type that associates values with the keys. The length of the array is not fixed, and the tangent can be any data type. For this reason, there are two types of arrays: indexed arrays and associative arrays.

indexed array: The keys value in the array is integer, which starts from 0 to (length-1); The values in the array can be accessed directly through the index;

Associative arrays:The data type of keys is either string type or direct access by index, but cannot be applied to traversal arrays like for loops.

Note: PHP arrays can contain both integer and string key names, because PHP does not actually differentiate between indexed arrays and associative arrays.

definition Syntax:

1 $arr _1=array (  keys1 = VALUES1,KEYS2 = Values22     //, ... Associative array 3); 4 $arr _2=array (values1,values25    //...). Index array 6); 7//Key (key) is an integer  or string String8//  value (value) can be any type of value

To iterate over an array:

1.for Loop: The For loop can only be used in an indexed array because the integer type is used to perform a for cycle.

Grammar:

1 for ($i =0; $i <count ($arr); $i + +) {};

2.foreach: Traversing an array, you can traverse any type of array, the most common one.

1 foreach ($arr as $values) {//Pair value Operation 2     echo $values. " "; 3}; 4 echo "<br/>"; 5 foreach ($arr as $key = + $values) {//Operate keys and values 6     echo $key. " = ". $values." "; 7}; 8/* 9 Results: 1 2 3 4 aa11 0=>1 1=>2 2=>3 3=>4 4=>aa12  

3.each () and list ():

Each (): Returns the current key/value pair in the array and moves the array pointer one step forward , after each () , the array pointer stays in the next cell in the array, or when it touches the end of the array, it stays in the last cell ( After executing the last one, the next execution will return an empty array). Returns an array that includes the keys and values of the array elements under the current pointer;

1 $arr =array (1,2,3,4, "AA"); 2 Print_r ($arr); 3 echo "<br/>"; 4 $arr 1=array ("One" =>10, "one" =>20, "three" and "AA"); 5 Print_r ($arr 1); 6 echo "<br/>"; 7 $arr 2=each ($arr); 8 Print_r ($arr 2); 9 echo "<br/>", 10//executed 3 times, one $arr 3=each ($arr 1), Print_r ($arr 3), echo "<br/>", $arr 3=each ($arr 1); 15 Print_r ($arr 3), echo "<br/>", $arr 3=each ($arr 1), Print_r ($arr 3);  

Results:

Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = AA)
Array ([one] [[one]] [+] = [three] = AA)
Array ([1] = 1 [value] = 1 [0] = 0 [key] = 0)

Array ([1] = [value] = [0] = = one [key] = one)//hands move forward
Array ([1] = [value] = [0] = [key] =-
Array ([1] = AA [value] = AA [0] = = three [key] = three)

List (): assigns the values in the array to some variables, like array (), which is not a real function, but a language structure.  list () assigns a set of variables in one-step operation. list () can only be used for arrays of numeric indexes and assumes that the numeric index starts at 0.

1 $arr =array (1,2,3,4, "AA"); 2 Print_r ($arr); 3 echo "<br/>"; 4 $arr 1=array ("One" =>10, "2" =>20, "three" = "AA"); 5 Print_r ($arr 1); 6 list ($first, $second, $three) = $arr; 7 echo "<br>"; 8 echo $first. " ". $second." ". $three; 9 list ($first, $second, $three) = $arr 1;10 echo "<br>", one echo $first. " ". $second." ". $three;//The assignment of key is not an integer echo" <br/> ", the list (,, $three) = $arr;//can skip input, cannot write, use, separate echo $three;

Results of the operation:

Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = AA)
Array ([One] = [2] = [three] = AA)
1 2 3
20
3

This place to review the Boolean type:

This is one of the simplest data types

To explicitly convert a value to a Boolean, use either (bool) or (Boolean) to cast. However, there are many cases where casting is not required because the value is automatically converted when an operator, function, or process control structure requires a boolean parameter.

See discriminant for type conversions.

When converted to Boolean, the following FALSE values are considered:

    • The Boolean FALSE value itself
    • Integer value 0 (0)
    • Floating-point value 0.0 (0)
    • An empty string, and the string "0"
    • An array that does not include any elements
    • Objects that do not include any member variables (PHP 4.0 only applies)
    • Special type NULL (including variables that have not been assigned)
    • SimpleXML object generated from an empty tag

All other values are considered to be TRUE (including any resources).

1 <?php 2 var_dump ((bool)  "");         BOOL (FALSE) 3  var_dump ((bool)  1);          BOOL (TRUE) 4  var_dump ((BOOL)-2);         BOOL (TRUE) 5  var_dump ((bool)  "foo");      BOOL (TRUE) 6  var_dump ((bool)  2.3e5);      BOOL (TRUE) 7  var_dump ((bool) array (n));  BOOL (TRUE) 8  var_dump ((BOOL) array ());    BOOL (FALSE) 9  var_dump ((bool)  "false");    BOOL (TRUE) 10  

This allows us to use the list () and each () to output the array, which is the same as the function of foreach, with access to both arrays

1 while (list ($key, $value) =each ($arr)) {//This place stops 2     echo $key when empty. " = ". $value." <br/> "; 3};4 while (list ($key, $value) =each ($arr)) {//no longer executed because the pointer is already the last 5     echo $key." = ". $value." <br/> "; 6};

Operation Result:

0=>1
1=>2
2=>3
3=>4
4=>5

For loop to implement this function

1 for ($i =0; $i <count ($arr), $i + +) {2     list ($a, $b) =each ($arr); 3     echo $a. " = ". $b." <br/> "; 4}

Operation Result:

0=>1
1=>2
2=>3
3=>4
4=>5

4. Using pointers to iterate through an array

Key ()-Returns the key in the array

Current ()-Returns the value currently in the array

End ()-Points the inner pointer of the array to the last cell

Prev ()-Returns the internal pointer of the array back to a

Next ()-Moves the inner pointer of the array forward one

Reset ()-Points the inner pointer of the array to the first cell, restoring

The parameters here are all arrays to be manipulated.

1 echo key ($arr);//returns the key under the current pointer of the current array; 2 echo "&nbsp;&nbsp;&nbsp;&nbsp;"; 3 echo current ($arr);//Returns values under the current pointer of an array; 4 echo "<br/>"; 5 Next ($arr);//Move down one pointer 6 echo key ($arr); 7 echo "&nbsp;&nbsp;&nbsp;&nbsp;"; 8 Echo current ($arr); 9 echo "<br/>", prev ($arr);//move forward one pointer to echo key ($arr); echo "&nbsp;&nbsp;&nbsp;&nbsp;"; Echo current ($arr), echo "<br/>", End ($arr),//move to the last Echo key ($arr), and Echo "&nbsp;&nbsp;& nbsp;&nbsp; "; Echo current ($arr), echo "<br/>", Reset ($arr);//move to the first echo key ($arr); echo "&nbsp;&nbsp;& nbsp;&nbsp; "; Echo current ($arr), echo "<br/>";

Operation Result:

0 1
1 2
0 1
4 5
0 1

Use combination to iterate through an array

1 $arr =array ("One" =>1, "one", "one" =>2, "three" =>3,4,5), 2 do{3     echo key ($arr). " ". Current ($arr)." <br/> 4}while (Next ($arr));

Operation Result:

One=>1
Two=>2
Three=>3
0=>4
1=>5

Pre-defined arrays:

  

Common functions:

(1) In_array (Value,array[,strict=true|false]);-Check whether an array has a value, return TRUE or False;strict=true strict type of check, meaning: 1 and "1" is not the same; Error returns null;

1 $arr =array ("One" =>1, "one", "=>2", "three" =>3,4,5), 2 var_dump (In_array ("1", $arr)),//bool (True) 3 Var_dump (In_ Array ("1", $arr, True)),//bool (False) 4 Var_dump (In_array ("1", $arr, false));//bool (True)

(2) array_reverse (array); Reverses an array, returning an array of cells opposite

1 $arr =array ("One" =>1, "one", "=>2", "three" =>3,4,5), 2 print_r ($arr); 3 echo "<br/>"; 4 Print_r (Array_ Reverse ($arr));

Results:

Array ([One] = 1 [One] = 2 [Three] = 3 [0] = 4 [1] = 5)
Array ([0] = 5 [1] = 4 [Three] = 3 [One] = 2 [one] = 1)

(3) count (array); Returns the number of array elements (key-value pairs);

1 $arr =array ("One" =>1, "one", "=>2", "three" =>3,4,5), 2 echo count ($arr);//5

(4) Array_unique (); Returns a new array of de-weight;

1 $arr =array ("One", "=>1", "=>2,4", "three" =>4,4), 2 $arr 1=array_unique ($arr); 3 Print_r ($arr 1);//b keep the front.

Results:

Array ([One] = 1 [One] = 2 [0] = 4)

(5) unset (array); no return value, just to modify the array----release the given variable

1 $arr =array ("One" =>1, "one" =>2,4, "three" =>4,4); 2 Print_r ($arr); 3 echo "<br/>"; 4 unset ($arr [' one ']); 5 Print_r ($arr); 6 unset ($arr); 7 Print_r ($arr);//array has been deleted 8 $a = 1; 9 echo "<br/>", Echo $a, unset ($a), echo $a;//variable has been deleted//unset ($a, $b, $c) destroy multiple variables at once

Operation Result:

The report has no variable warning, stating that the variable has been destroyed

(6) array_values (array); Returns all the values in the array and establishes a numeric index on them.

1 $arr =array ("One" =>1, "one", "=>2,4", "three" =>4,4), 2 print_r ($arr); 3 echo "<br/>"; 4 Print_r (array_values ($arr));

Operation Result:

Array ([One] = 1 [One] = 2 [0] = 4 [Three] = 4 [1] = 4)
Array ([0] = 1 [1] = 2 [2] = 4 [3] = 4 [4] = 4)

(7) Array_merge (Array1,array2 ...); Merges multiple arrays, returning the merged array

1 $arr =array ("One" =>1, "one" =>2,4, "three" =>4,4), 2 Print_r (Array_merge ($arr, $arr));

Operation Result:

Array ([One] = 1 [One] = 2 [0] = 4 [Three] = 4 [1] = 4 [2] = 4 [3] = 4)

(8) Array_push (); Adds an element after the array, returns the number of arrays after the element is added , changes the original array

1 $arr =array ("One" =>1, "one", "=>2,4", "three" =>4,4), 2 Print_r ($arr), 3 echo "<br>", 4 echo Count ($arr); 5 echo "<br>"; 6 echo Array_push ($arr, 2); 7 echo "<br>"; 8 Print_r ($arr);

Operation Result:

Array ([One] = 1 [One] = 2 [0] = 4 [Three] = 4 [1] = 4)
5
6
Array ([One] = 1 [One] = 2 [0] = 4 [Three] = 4 [1] = 4 [2] = 2) php

PHP Collation (ii): arrays

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.