PHP basics-array and data structure, php array data structure
Three features of strong language: 1. Only data of the same data type can be stored in the array. 2. The length of the array is fixed. 3. It is continuous in the memory.
I. array definition and Value
$ Arr = array (1, 2, 3, 4, 5 );
// Echo $ arr [0]; // Index Array
$ Arr1 = array ("one" => "111", "two" = & gt; "222 ");
// Echo $ arr1 ["one"]; // associate an array
2. traverse the Array
1. for Loop traversal, only index arrays can be traversed
For ($ I = o; $ I <count ($ arr); $ I ++)
{
Echo $ arr [$ I]. "<br> ";
};
2. foreach loop traversal, which can traverse index arrays and associated arrays (recommended)
Foreach ($ arr1 as $ k => $ v)
{
Echo $ k. "---". $ v. "<br> ";
}
Foreach (int a in $ arr ){};
3. Combine list () and each () for Traversal
(1) var_dump (each ($ arr); // retrieves the element pointed to by the current pointer, returns a new array (this array contains the key and value), and calls a pointer back
Var_dump (each ($ arr ));
Var_dump (each ($ arr ));
(2) list ($ a, $ B, $ c) = $ arr; // assign the elements of the array on the right to a group of variables.
Echo $ c;
List ($ k, $ v) = each ($ arr );
While (list ($ k, $ v) = each ($ arr ))
{Echo "{$ k}-{$ v} <br> ";}
4. pointer Traversal
Echo current ($ arr); // obtain the value of the element pointed to by the current pointer.
Echo key ($ arr); // retrieves the index of the element pointed to by the current pointer.
Next ($ arr); // lower the pointer
Prev ($ arr); // tune the pointer up
Reset ($ arr); // reset the pointer
End ($ arr); // point the pointer to the last one
While (list ($ k, $ v) = each ($ arr ))
{Echo "{$ k}-{$ v} <br> ";}
Reset ($ arr );
While (list ($ k, $ v) = each ($ arr ))
{Echo "{$ k}-{$ v} <br> ";}
3. array Functions
$ Arr1 = array ("one" => "111", "two" = & gt; "222 ");
1. echo $ arr1 ["one"]; // associate an array
2. unset ($ arr [2]); // delete an array element
3. $ arr [2] = 10; // Add an element
4. $ arr [] = 10; // append an element
5. arry_push ($ arr, 12); // append Element
Var_dump ($ arr );
6. count ($ arr); // array Length
7. var_dump (in_array (2, $ arr); // check whether a value exists in the array.
8. var_dump (array_reverse ($ arr); // reverse the elements in the array sequentially, create a new array, and return
9. $ arr = array_unique ($ arr); // Delete the repeated values in the array and return the new array.
10. var_dump (array_values ($ arr); // re-Index
11. var_dump (array_merge ($ arr, $ arr1); // merge Arrays
4. pre-defined array