6-6-1-php array Correlation (2)

Source: Internet
Author: User

1.for less used to traverse the array of successive numbers subscript, and C language is similar, simple record:

for (int $i =0; $i <count ($arr), $i + +) {... $arr [$i] ...}

2.php A foreach statement designed to iterate over an array

foreach ($arr as $value) {}

foreach ($arr as $key = + $value) {}

Multidimensional: Using nested

foreach ($arr as $key = + $arr 1) {

foreach ($arr 1 as $value) {...} $value ...}

}

3. Use the each () function

Each ($arr) returns an array of associative and indexed blends with four elements

and moves the array pointer until it returns false

4.list ()

To assign a value to an array:

Array ("Coffee","Brown","caffeine");

List ($a,, $c) = $info;

Echo $c; Caffeine

Note: The list can only be applied to arrays of numeric indexes and assumes that the index starts at 0. The index has the use of a later while (list () =each ()).

5.each () and list () loop use

while (list ($key, $value) = each ($arr)) {}

After iterating through the array using while, the each statement has pointed the pointer at the end of the array. Using the while statement again requires reset ()

6. Iterating through an array using the array internal pointer control function

7. Array-related functions

Array_values ($contacts); Array, return all values

Array_keys ($contacts); Array that returns all the health values

7.1.1:

In_array (mixed needle, array haystack, [bool strict]); Finds whether a value is in the specified array, returns a bool

Needle: The value to look for (simple data type, array, etc.),

Haystack: An array of lookups,

Strict: Very strict definition of data type lookup.

The In_array function distinguishes the case of find content.

7.1.2

$key _name = Array_search ("$value", $array)//parameter is the same as above, check if a value is in it, and return its health.

7.1.3

BOOL Array_key_exists ("Key_name", $array); Checks if the array exists for the specified health

A null value in an array returns TRUE. and Isset () will.

7.2

function $Array = Array_flip ($Array); Reverses the health and value of an array

If a value occurs more than once, the last duplicate value overrides the previous key-value pair.

If the value is not a string or an array type, an error is found.

7.3 Function $Array = Array_reverse ($Array, bool lose_key); Reverse array element Order

The second parameter is optional, and if true, the key name is lost.

8. Number and uniqueness of statistical elements

8.1 function count (), sizeof () is his alias, the function is the same

int count ($var, BOOL mode)

The first value is the array or object passed in to be counted, the second argument is optional, 0 or 1, and whether the number of elements in the multidimensional array is recursively queried.

Count ($web, 1) = 10;

8.2 Function $Array = array_count_values ($ array)//number of occurrences of values in the statistics array

8.3 Function $Array = Array_unique ($Array); Removes duplicate values from the array and returns a new array with no duplicates.

The function first sorts the values as strings, and then each value retains the value of the key name that first appears. Note that sorting first is not the first one that appears.

9.5.3 functions for handling arrays using callback functions

1. Array array_fliter (array input, "callback function");

The first parameter is an array that needs to be filtered, and the second parameter is to pass in a custom function as a string.

The value of the array of the first parameter is passed in the first function, and if True is returned, the corresponding health value

Will pass in the newly formed array

2.array_walk ()

BOOL Array_walk (array &array, callback FuncName, (mixed data));

The first one is the array to be processed, the second is the callback function,

The parameters that the function receives are: The value of the element, the key of the element, (mixed data)

If you need to directly act on the value of the array, specify the first argument of the callback function as a reference,& $value

Function myfunc3 (& $value, $key) {

$value = "web";

}

Array_walk ($arr, "myfunc3");

Array_map, which is used for multiple arrays.

9.5.4 Array Sorting

1.

Sort (array $array, [Sort_flag]); Ascending

Rsort (Array $array, [Sort_flag]);

This method key name is ignored, and the number is re-indexed in sequential re-index array.

Sort_flag: Specifies the sort type.

2.

Ksort ()//Sort by key name

Krsort ()

3.

Asort ()/arsort ()//Sort by value

The key name is reserved compared to the sort ()

9.5.5 splitting and merging arrays

1.

Array array_slice (array array, int offset [, int length[,bool Preserve_keys]])

Go to a certain value and return

2.

Array Array_splice (array &input, int offset, [, length [, replacement]])

Remove or replace

3.

Array_combine (array key, array value)

Merge array, first array as key, second array as value, two parameters must have the same number of elements

4.

Array_merge (array array1 [, array array2 [, array array3]])

The connection array.

If the key name is duplicated, the last one will be overwritten.

If the key name is an integer, it will be re-indexed starting at 0.

5.

Array_intersect (Array1, Array2, [, array3 ...])

Intersection.

Key names remain unchanged, only values are compared

6.

Array_diff (Array1, Array2, [, array3 ...])

Subtraction

9.5.7 some other useful array-handling functions

1. Mixed Array_rand (array input [, int num_req]))

Randomly selects one or more elements from the array to return.

2.shuffle () randomly reorder array elements

3.array_sum () returns the sum of all the values in the array

4.array Range (mixed first, mixed second, [, num step])

Creates and returns an array of the specified range.

6-6-1-php array Correlation (2)

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.