Introduction to several new functions of array Library php_php tutorial

Source: Internet
Author: User
Tags sorts
We do not have a lot of PHP data, we do not have a copy of the Php4gb.chm. I admire the function library part of it most, the real online help. But the pace of PHP development is so fast, you sui, I recently found some extended array functions in www.php.net/manual/.

Let me introduce them to you, my English level is not high, there is the wrong place to translate, please correct me.
The format is this:

Function name Support version

function declaration
Description and parameters, return value

Example


Ok,let ' s go.

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

Array Array_flip (array trans)

The key and value of the array trans are exchanged, that is, key becomes value, and value becomes key.
Returns an array of finished processing.

Cases:
$a [0]= "ABC";
$a [1]= "def";
After a 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 individual values in the input array. Returns an array with the value of input as key to the new array with the number value.

Cases:
$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 the array2 to the back of the array1. Returns an array of results.
If it is an associative array, with a string of key, a key with the same name appears, then the following will overwrite the previous one, and the subscript array will not overwrite, just add it later.

Cases:
$array 1 = Array ("Color" = "Red", 2, 4);
$array 2 = Array ("A", "B", "color" = "green", "shape" = "trapezoid", 4);
Array_merge ($array 1, $array 2);
Resulting array would 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 ...])
Recursive merging of arrays is basically similar to the previous function. The difference is that, in terms of associative arrays, it does not simply merge the same key or generate a two-dimensional array to merge the value of the same key. (not clearly expressed, sorry, see example).

Cases:
$ar 1 = Array ("color" = = Array ("Favorite" = "Red"), 5);
$ar 2 = Array ("color" = = Array ("Favorite" = "green", "Blue"));
$result = array_merge_recursive ($ar 1, $ar 2);

Resulting array would be an array ("color" = = Array ("favorite" = = Array ("Red", "green"), "Blue"), 5, 10).


Do you understand? The Red,green is merged into a new array, placed in the favorite.

See also Array_merge ().

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

Array Array_intersect (array array1, array array2 [, array ...])
Takes the intersection of multiple arrays and returns a new array containing the intersection elements.
Based on array1, so, if it is an associative array, then the key value is array1. See example.

Cases:
$array 1 = Array ("A" = "green", "Red", "blue");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);
This makes $result has an array ("a" = "green", "red");

See also Array_diff ().

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

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

In contrast to the previous function, this is the difference between multiple arrays.

Cases:
$array 1 = Array ("A" = "green", "Red", "blue");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_diff ($array 1, $array 2);

This makes $result has an 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)

The two functions are related and put together.
Array_keys can take out all the keys of an array, and if search_value is defined, only the corresponding key value is taken.
Array_values all value values of the array input are taken out.

Cases:

$array = Array ("Size" = "XL", "color" = "gold");
Array_values ($array); Returns Array ("XL", "gold")

$array = Array (0 = +, "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 sorts multiple dimensions on a multidimensional array. (Very useful, oh, I asked this question last in the Chinese user).

The input array is processed into the columns of the table, sorted by row, somewhat similar to the order by condition in the SQL statement.
The parameters of this function are not common, but are flexible. But an array or some of the following flags.

SORT_ASC-Ascending

Sort_desc-Descending

Sort_regular-General comparison

Sort_numeric-Numerical comparison

Sort_string-string comparison


An array can not give two types of sort flags at the same time (this of course). The flags after each array are valid only for this array. The default is SORT_ASC and Sort_regular.

Returns true if normal, otherwise false is returned.

Example 1:
$ar 1 = Array ("Ten", "N", "a");
$ar 2 = Array (1, 3, "2", 1);
Array_multisort ($ar 1, $ar 2);

The result is $ar 1 = ten, "A", 100, 100. $ar 2 = 1, 1, 2, "3".

Example 2:
$ar = Array (Array ("Ten", "+", "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 would contain, "a" (it is sorted as strings in ascending order), and the SE Cond one would contain 1, 3, "2", 1 (sorted as numbers, in descending order).

However, the above example I tried, no, it is reported that the parameter 3 requirements is an array of errors. (??? I don't even have a word for that.)

If you are directly using Array_multisort ($ar [0],sort_asc, $ar [1],sort_desc];


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

Mixed Array_pop (array array)
int Array_push (array array, mixed var [, mixed ...])
Mixed Array_shift (array array)
int Array_unshift (array array, mixed var [, mixed ...])

An array of functions to use as a stack. The use of the concrete is relatively simple:

Pop pops up the last element, returning the element value.
Push adds the parameter var to the array at the end. Returns the position. Same function as $array[]= $var. Returns the number of new elements in the array.
Shift pops the first element of the array, and the other moves one bit, equivalent to the left shift. But the number of array elements is reduced by 1. Returns the popup element.
Unshift adds one or more elements to the array before returning the number of new arrays.


Example 1. Array_pop () example

$stack = Array ("Orange", "apple", "raspberry");
$fruit = Array_pop ($stack);
After this, $stack have only 2 elements: "Orange" and "Apple", and $fruit have "raspberry".


Example 2. Array_push () example

$stack = Array (1, 2);
Array_push ($stack, "+", 3);
This example would the result in $stack has 4 elements:1, 2, "+", and 3.

Example 3. Array_shift () example

$args = Array ("-V", "-f");
$opt = Array_shift ($args);
This would the result in $args have one element "-F" left, and $opt being "-V".


Example 4. Array_unshift () example

$queue = Array ("P1", "P3");
Array_unshift ($queue, "P4", "P5", "P6");
This would result in $queue has 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 an array. The parameter num_req gives the number of elements to be selected, which defaults to 1.
Returns an array that contains the key of the selected element.

First call Srand () to generate a random number 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 that takes the elements of input in reverse order.

Example 1. Array_reverse () example

$input = Array ("PHP", 4.0, Array ("Green", "red"));
$result = Array_reverse ($input);
This makes $result has an array ("Green", "Red"), 4.0, "PHP").

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

Array_slice (PHP4)

Array array_slice (array array, int offset [, int length])
Takes a portion of an array, starting with offset, length, and default to end.
Returns a new array.

Offset is positive, starting at the offset position of the array, or negative, counting from the end of the array.
The length is positive, the new array is a negative, and 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 from the array, starting with offset, length of the part, if the replacement[] parameter is given, then use this parameter to replace the removed part.

For the processing of offset, length, the same as the previous example.
If there is a replacement parameter, this parameter is used instead of the remove part, and if it is not removed, it is inserted at offset position.

The following actions 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 array)

Removes the duplicate value from an array. Returns the new array.
If it is an associative array, key is the first one.

Example 1. Array_unique () example

$input = Array ("A" = "green", "Red", "b" = "green", "Blue", "Red");
$result = Array_unique ($input);
This makes $result has an array ("a" = "green", "Red", "Blue");.

http://www.bkjia.com/PHPjc/317537.html www.bkjia.com true http://www.bkjia.com/PHPjc/317537.html techarticle We do not have a lot of PHP data, we do not have a copy of the Php4gb.chm. I admire the function library part of it most, the real online help. But the pace of development of PHP is too fast, ...

  • 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.