Introduction of new function php_php techniques for several array libraries

Source: Internet
Author: User
Tags arrays mixed rand
We don't have a lot of PHP information on hand, we have a php4gb.chm. I most appreciate the library part of it, 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/.

Below I introduce them to everybody, my English level is not high, has the translation wrong place, 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, value exchange of the array trans is the key variable value, and the value becomes key.
Returns an array of finished processing.

Cases:
$a [0]= "ABC";
$a [1]= "def";
After a array_flip () you:
$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 value of input as the 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 ...])
Merges multiple arrays, adding 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 as key, and a key with the same name appears, the following will overwrite the previous one, and the subscript array will not appear overwritten, just add it to the back.

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

also Array_merge_recursive ().

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

Array array_merge_recursive (array array1, array array2 [, array ...])
Recursively merges an array, essentially similar to the previous function. The difference is that in the associative array, it is not simply merging the same key or generating a two-dimensional array to merge the value of the same key. (The expression is not clear, sorry, see examples).

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 array ("COLOR" => Array ("Favorite" => Array ("Red", "green"), "Blue", 5, 10).


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

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 that contains the elements of the intersection.
Based on array1, so, if it's an associative array, the key value is array1. See examples.

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 have array ("a" => "green", "red");

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 set of differences for 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 have array ("blue");

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 takes out all value values for array input.

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

Sort multiple arrays at the same time, or multiple dimensions for a multidimensional array. (Very useful, my last time in Chinese users asked this question).

The input array is processed into a table column, sorted by row, somewhat analogous to an order by condition in an SQL statement.
The parameters of this function are not common, but they 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 cannot be given two types of sort flags at the same time (this is of course). The flag after each array is valid only for this array. The default is SORT_ASC and Sort_regular.

Returns true if normal, otherwise returns false.

Example 1:
$ar 1 = Array ("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 ("Ten", "N", "a"), Array (1, 3, "2", 1));
Array_multisort ($ar [0], SORT_ASC, sort_string,
$ar [1], sort_numeric, SORT_DESC);

After sorting, the "I" 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, will report parameter 3 is an array of errors. (??? I don't even have one.

If you use Array_multisort directly ($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 ...])

The array is used as a function of the stack. The use of the concrete is relatively simple:

POPs pops up the last element and returns the element value.
Push adds the parameter var to the array at the end. return position. Same as $array[]= $var function. Returns the number of new elements in the array.
Shift to eject the first element of the array, and the other to move one position, equivalent to the left offset. But the number of array elements is reduced by 1. Returns the element that pops up.
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, the $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 would 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 result in $args has 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 select one or more elements from an array. The parameter num_req gives the number of elements to be selected, the default is 1.
Returns an array that contains the key of the selected element.

First call Srand () to produce random number of seeds.

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 have array (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 at offset, length, and default to end.
Returns a new array.

Offset is positive, starting at the offset position of the array, if negative, counting from the end of the array.
Length is positive, is a new array of lengths, is negative, is also from the end of the array reciprocal.

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]])

Removes the part from the array that begins with offset and length, and replaces the removed part with this parameter if the replacement[] argument is given.

The processing judgment for offset and length is 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 the 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 duplicate values from an array. Returns the new array.
If it is an associative array, the key is the first.

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.