PHP basics-array and data structure, php array data structure

Source: Internet
Author: User

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

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.