Introduce several new functions of array library php

Source: Internet
Author: User
Tags random seed
Introducing several new functions of the array library php we don't have much information on hand about PHP. do you have a copy of php4gb. chm. I like the function library in it most, and it provides online help. However, PHP is developing too fast. you know, I recently found some extended array functions at www.php.net/manual.

Let me introduce them to you. I have a low level of English. please correct me if the translation is incorrect.
The format is as follows:

Supported function name versions

Function declaration
Description and parameters and return values

Example


OK, Let's go.

//*************************
Array_flip (PHP4> = 4.0b4)

Array array_flip (array trans)

Switch the key and value of the array trans, that is, change the key to the value, and change the value to the key.
Returns the array of processed items.

Example:
$ A [0] = "abc ";
$ A [1] = "def ";
After an array_flip () you get:
$ A ["abc"] = 0; $ a ["def"] = 1;

//***************************
Array_count_values (PHP4> = 4.0b4)

Array array_count_values (array input)
Counts the number of values in the input array. Returns an array with the input value as the key and a new array with the number as the value.

Example:
$ Array = array (1, "hello", 1, "world", "hello ");
Array_count_values ($ array );
// Returns array (1 => 2, "hello" => 2, "world" => 1)

//*****************************
Array_merge (PHP4)

Array array_merge (array array1, array array2 [, array...])
Merge multiple arrays and add the contents of array2 to the end of array1. Returns an array of results.
If it is an associated array, the string is the key, and the key with the same name appears, the subsequent will overwrite the previous one, and the subscript array will not overwrite, just add it to the back.

Example:
$ Array1 = array ("color" => "red", 2, 4 );
$ Array2 = array ("a", "B", "color" => "green", "shape" => "trapezoid", 4 );
Array_merge ($ array1, $ array2 );
// Resulting array will be array ("color" => "green", 2, 4, "a", "B", "shape" => "trapezoid ", 4 ).

See also array_merge_recursive ().

//******************************
Array_merge_recursive (PHP4> = 4.0.1)

Array array_merge_recursive (array array1, array array2 [, array...])
Recursively merges arrays, which is similar to the previous function. The difference is that it does not simply merge the same key, or generate a two-dimensional array to merge the values of the same key. (Sorry, let's look at the example ).

Example:
$ Ar1 = array ("color" => array ("favorite" => "red"), 5 );
$ Ar2 = array (10, "color" => array ("favorite" => "green", "blue "));
$ Result = array_merge_recursive ($ ar1, $ ar2 );

// Resulting array will be array ("color" => array ("favorite" => array ("red", "green"), "blue"), 5, 10 ).


Do you understand? Red and green are merged into a new array and placed in favorite.

See also array_merge ().

//*******************************
Array_intersect (PHP4> = 4.0.1)

Array array_intersect (array array1, array array2 [, array...])
Returns a new array containing the intersection elements.
It is based on array1, so if it is an associated array, the key value is of array1. See the example.

Example:
$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_intersect ($ array1, $ array2 );
// This makes $ result have array ("a" => "green", "red ");

See also array_diff ().

//************************************** *****
Array_diff (PHP4> = 4.0.1)

Array array_diff (array array1, array array2 [, array...])

Opposite to the previous function, this is the difference set of multiple arrays.

Example:
$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_diff ($ array1, $ array2 );

// This makes $ result have array ("blue ");

See also array_intersect ().

//************************************** *****
Array_keys (PHP4)
Array_values (PHP4)

Array array_keys (array input [, mixed search_value])
Array array_values (array input)

These two functions are related and put together.
Array_keys can retrieve all the keys of the array. if search_value is defined, only the corresponding key value is obtained.
Array_values retrieves all value values of array input.

Example:

$ Array = array ("size" => "XL", "color" => "gold ");
Array_values ($ array); // returns array ("XL", "gold ")

$ Array = array (0 => 100, "color" => "red ");
Array_keys ($ array); // returns array (0, "color ")

$ Array = array ("blue", "red", "green", "blue", "blue ");
Array_keys ($ array, "blue"); // returns array (0, 3, 4)


//************************************** ********
Array_multisort (PHP4> = 4.0b4)

Bool array_multisort (array ar1 [, mixed arg [, mixed... [, array...])

Sorts multiple arrays at the same time or multiple dimensions in a multi-dimensional array. (Very useful. I asked this question when I was a Chinese user ).

The input array is processed into columns in the table and sorted by rows, which is similar to the order by condition in SQL statements.
The parameters of this function are not common, but flexible. But an array or the following signs.

SORT_ASC-ascending

SORT_DESC-descending order

SORT_REGULAR-regular comparison

SORT_NUMERIC-numerical comparison

SORT_STRING-string comparison


An array cannot give two types of sorting signs at the same time (of course ). The flag after each array is only valid for this array. The default values are SORT_ASC and SORT_REGULAR.

If it is normal, true is returned; otherwise, false is returned.

Example 1:
$ Ar1 = array ("ten", 100,100, "");
$ Ar2 = array (1, 3, "2", 1 );
Array_multisort ($ ar1, $ ar2 );

// The result is $ ar1 = 10, "a", 100,100. $ ar2 = 1, 1, 2, "3 ".

Example 2:
$ Ar = array ("10", 100,100, "a"), array (1, 3, "2", 1 ));
Array_multisort ($ ar [0], SORT_ASC, SORT_STRING,
$ Ar [1], SORT_NUMERIC, SORT_DESC );

// After sorting, the first array will contain 10,100,100, "a" (it was sorted as strings in ascending order), and the second one will contain in 1, 3, "2 ", 1 (sorted as numbers, in descending order ).

However, I tried it in the above example. no, the array error is required for parameter 3. (??? I don't know)

If you directly use array_multisort ($ ar [0], SORT_ASC, $ ar [1], SORT_DESC); yes.


//************************************** ****
Array_pop (PHP4)
Array_push
Array_shift
Array_unshift

Mixed array_pop (array)
Int array_push (array, mixed var [, mixed...])
Mixed array_shift (array)
Int array_unshift (array, mixed var [, mixed...])

Array is used as a function of the stack. It is easy to use:

Pop pops up the last element and returns the element value.
Push adds the var parameter to the end of the array. Return location. Same as $ array [] = $ var. Returns the number of new elements in the array.
Shift pops up the first element of the array. the other elements are moved one by one, which is equivalent to the left displacement. But the number of array elements is reduced by 1. Returns the pop-up element.
Unshift adds one or more elements to the front of the array to return the number of new arrays.


Example 1. Array_pop () example

$ Stack = array ("orange", "apple", "raspberry ");
$ Fruit = array_pop ($ stack );
// After this, $ stack has only 2 elements: "orange" and "apple", and $ fruit has "raspberry ".


Example 2. Array_push () example

$ Stack = array (1, 2 );
Array_push ($ stack, "+", 3 );
// This example wocould result in $ stack having 4 elements: 1, 2, "+", and 3.

Example 3. Array_shift () example

$ Args = array ("-v", "-f ");
$ Opt = array_shift ($ args );
// This wowould result in $ args having one element "-f" left, and $ opt being "-v ".


Example 4. Array_unshift () example

$ Queue = array ("p1", "p3 ");
Array_unshift ($ queue, "p4", "p5", "p6 ");
// This wowould result in $ queue having 5 elements: "p4", "p5", "p6", "p1", and "p3 ".


//************************************** *
Array_rand (PHP4> = 4.0.0)

Mixed array_rand (array input [, int num_req])

Randomly selects one or more elements from the array. The num_req parameter specifies the number of elements to be selected. the default value is 1.
Returns an array with the key of the selected element.

Srand () must be called in advance to generate random seed.

Example 1. Array_rand () example

Srand (double) microtime () * 10000000 );
$ Input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank ");
$ Rand_keys = array_rand ($ input, 2 );
Print $ input [$ rand_keys [0]. "\ n ";
Print $ input [$ rand_keys [1]. "\ n ";

//**************************************
Array_reverse (PHP4> = 4.0b4)

Array array_reverse (array input)
Returns a new array in reverse order of input elements.

Example 1. Array_reverse () example

$ Input = array ("php", 4.0, array ("green", "red "));
$ Result = array_reverse ($ input );
// This makes $ result have array ("green", "red"), 4.0, "php ").

//************************************** **

Array_slice (PHP4)

Array array_slice (array, int offset [, int length])
Take part of an array, starting from offset and its length is length. the default value is end.
Returns a new array.

If the offset value is positive, it starts from the offset position of the array. if it is negative, it starts from the end of the array.
The length is positive. it is the length of the new array. if it is negative, it is also the reciprocal from the end of the array.

Example 1. Array_slice () examples

$ Input = array ("a", "B", "c", "d", "e ");

$ Output = array_slice ($ input, 2); // returns "c", "d", and "e"
$ Output = array_slice ($ input, 2,-1); // returns "c", "d"
$ Output = array_slice ($ input,-2, 1); // returns "d"
$ Output = array_slice ($ input, 0, 3); // returns "a", "B", and "c"

//************************************** ****

Array_splice (PHP4)

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

Remove the part of length from the array starting from offset. if the replacement [] parameter is provided, use this parameter to replace the removed part.

The processing and determination of offset and length is the same as that in the previous example.
If the replacement parameter exists, this parameter is used to replace the removed part. if the parameter is not removed, it is inserted at the offset position.

The following operations are equivalent:
Array_push ($ input, $ x, $ y) array_splice ($ input, count ($ input), 0,
Array ($ x, $ y ))
Array_pop ($ input) array_splice ($ input,-1)
Array_shift ($ input) array_splice ($ input, 0, 1)
Array_unshift ($ input, $ x, $ y) array_splice ($ input, 0, 0, array ($ x, $ y ))
$ A [$ x] = $ y array_splice ($ input, $ x, 1, $ y)

Returns a new array containing the removed elements.

Example 1. Array_splice () examples

$ Input = array ("red", "green", "blue", "yellow ");

Array_splice ($ input, 2); // $ input is now array ("red", "green ")
Array_splice ($ input, 1,-1); // $ input is now array ("red", "yellow ")
Array_splice ($ input, 1, count ($ input), "orange ");
// $ Input is now array ("red", "orange ")
Array_splice ($ input,-1, 1, array ("black", "maroon "));
// $ Input is now array ("red", "green ",
// "Blue", "black", "maroon ")

//***********************
Array_unique (PHP4> = 4.0.1)

Array array_unique (array)

Removes duplicate values from an array. Returns a new array.
If it is an associated array, the first key prevails.

Example 1. Array_unique () example

$ Input = array ("a" => "green", "red", "B" => "green", "blue", "red ");
$ Result = array_unique ($ input );
// This makes $ result have array ("a" => "green", "red", "blue ");.

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.