The 3rd chapter uses arrays

Source: Internet
Author: User

1. Create and initialize arrays: $products =array (' Tires ', ' oil ', ' Spark plugs ');

Array () is a language result as well as Echo

$numbers =range (1,10,2); The range () function creates a 1-10 numeric array with a difference of 2 in ascending order

$letters =range (' A ', ' Z '); Range () also allows you to manipulate characters

The size of the array in 2.PHP will change dynamically depending on how many elements are added;

such as: $products [0]= ' Tres '; If the $products array does not exist, this code will create an array with only one element

3. Iterate through the array: for ();   or foreach (); foreach ($products as $current) {echo $current. “; }

4. Associative arrays: A bit like a map;

-Created: $prices =array (' Tires ' =>100, ' oil ' =>10); The symbol = = is the connector between the keyword and the value

$prices [' Spark plugs ']=4;

-Access: $val = $prices [' oil '];

-Cycle: ①foreach ($prices as $key + $value) {echo $key. "–". $value. " <br/> "; }

②while ($ele =each ($prices) {echo $ele [' key ']. ' – '. $ele [' value ']. " <br/> "; }

③reset ($prices);

while (list ($product, $price) =each ($prices) {echo "$product-$price <br/>";}

When you use the each () function, the array records the current element

Use the Reset () function to reset the current element to the beginning of the array

5. Sort:

(i) Ascending:

①sort (); Normal one-dimensional array ordering, second parameter: sort_regular (default), Sort_numeric (numeric), sort_string (String) specify the sort type;

②asort (); Sort by element value;

③ksort (); Sort by keywords;

(ii) Descending:

Correspondence: Rsort () Arsort () Krsort ()

(iii) Custom sorting:

Usort ($products, ' compare ');

$products is a sorted array, compare is a user-defined sort function compare (...). { ... }

(iv) Reordering:

①shuffle (); Randomly sort the elements of an array

②array_reverse (); Returns the array's inverse array (note: The original array is returned with the modified copy, you can choose to overwrite the original version or not overwrite)

The Array_push () function adds a new element to the end of the array, and the Array_pop () function deletes and returns the element at the end of the array

6. Other array operations:

①current () Returns the value of the current element (the POS () function is its alias);

②next () & each () moves the pointer forward one element, but each () returns the current element before the pointer moves forward, and next () moves the pointer forward and returns the new current element;

③prev (), in contrast to next (), moves the pointer back one position and returns the new current element;

④reset () & End () returns a pointer to the first element of the array, which returns a pointer to the end of the array;

7.array_walk (Arr,func,[userdata]);

Func (Value,key,userdata);

For each element of arr, the function func is applied, and the third parameter is used to transfer the function func with the parameter UserData;

Value is passed by reference when you want to modify the contents of an array (&)

8. Count the number of array elements:

Count () & sizeof () counts the number of elements in the array;

Array_count_values ($arr); Returns an array whose keywords are $arr element values, and the value of the array is the number of times the corresponding keyword has occurred in $arr

9. Extract the array elements and convert them into a series of variables:

$array =array (' key1 ' = ' value1 ', ' key2 ' = ' value2 ');

Extract ($array);

echo "$key 1 $key 2"; Converts the array keyword to a variable name, and the value of the keyword corresponds to the value of the converted Variable

Output: value1 value2 value3

* This function also has two optional parameters extract_type and Prefix,extract_type tell extract () How to handle the conflict

The most common values are: Extr_overwrite (default, overwrite existing variables when conflicting),

Extr_prefix_all (the value specified by the PREFIX parameter is preceded by all variable names);

Such as:

$array =array (' key1 ' = ' value1 ', ' key2 ' = ' value2 ');

Extract ($array, Extr_prefix_all, ' my_prefix ');

echo "$my _prefix_key1 $my _prefix_key2";

The 3rd chapter uses 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.