Home  >  Article  >  Backend Development  >  Introducing several new functions of array library php

Introducing several new functions of array library php

WBOY
WBOYOriginal
2016-07-29 08:35:51891browse

We don’t have much PHP information on hand. Does everyone have a copy of php4gb.chm? What I appreciate the most is the function library part, the real online help. But the pace of PHP development is too fast. Look, I recently found some extended array functions at www.php.net/manual/.
Now let me introduce them to you. My English level is not high. Please correct me if there are any mistakes in translation.
The format is like this:
Function name Supported version
Function declaration
Description, parameters, return value
Example
OK, Let's go.
//****************** *******
array_flip (PHP4 >= 4.0b4)
array array_flip (array trans)
Exchange the key and value of the array trans, that is, the key becomes value, and the value becomes key.
Returns the processed array.
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)
Count the number of each value in the input array. Returns an array with the input value as the key and a new array with the number of occurrences 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 after array1. Returns an array of results.
If it is an associative array, with a string as the key, and a key with the same name appears, the latter one will overwrite the previous one, and the subscript array will not be overwritten, but will be added at the end.
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 merge arrays, basically similar to the previous function. The difference is that in terms of associative arrays, it does not simply merge the same keys, but generates a two-dimensional array to merge the values ​​of the same keys. (Sorry for not being able to express clearly, let’s look at examples).
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 ...])
Take the intersection of multiple arrays and return a new array containing the intersection elements.
Based on array1, so if it is an associative array, the key value is array1. See examples.
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...])
Contrary 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 will be retrieved.
array_values ​​takes out all the values ​​​​of the 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 ...]]])
Sort multiple arrays at the same time, or sort a multi-dimensional array in multiple dimensions. (Very useful, I asked this question last time as a Chinese user).
The input array is processed into table columns and sorted by row, somewhat similar to the order by condition in the SQL statement.
The parameters of this function are uncommon, but very flexible. But it is an array or the following flags.
SORT_ASC - Ascending order
SORT_DESC - Descending order
SORT_REGULAR - Regular comparison
SORT_NUMERIC - Numeric comparison
SORT_STRING - String comparison
An array cannot be given two types of sort flags at the same time (of course). The flags after each array are only valid for this array. The defaults are SORT_ASC and SORT_REGULAR.
If normal, return true, otherwise return false.
Example 1:
$ar1 = array ("10", 100, 100, "a");
$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 (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 1, 3, "2", 1 (sorted as numbers, in descending order) .
However, I tried the above example, but it didn’t work. An error that parameter 3 is required to be an array will be reported. (???I don’t know this either)
If you directly use array_multisort($ar[0],SORT_ASC,$ar[1],SORT_DESC); you can.
//******************************************
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 . ..])
A function that uses arrays as stacks. The specific use is relatively simple:
pop pops up the last element and returns the element value.
push adds the parameter var to the end of the array. Return to location. Same function as $array[]=$var. Returns the new number of elements in the array.
Shift pops the first element of the array and shifts the others one bit forward, which is equivalent to a left shift. But the number of array elements is reduced by 1. Returns the popped element.
unshift adds one or more elements in front of the array and returns the new array number. 
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 would result in $stack having 4 elements: 1, 2, "+", and 3. 
Example 3. Array_shift() example 
$args = array ("-v", "-f"); 
$opt = array_shift ($args); 
//This would 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 would 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]) 
从数组中随机选出一个或多个元素。参数num_req给出要选出的元素个数,缺省为1。 
返回一个数组,内容是所选出元素的key。 
事先要先调用 srand() 来产生随机数种子。 
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) 
返回一个新数组,把input的元素取相反次序。 
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]) 
取一个数组的一部分,从offset开始,长度为length,缺省为到结束。 
返回一个新数组。 
offset若为正,从数组的offset位置开始,若为负,则从数组的结尾倒数算起。 
length为正,是新数组的长度,为负,则也是从数组的结尾倒数。 
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]]) 
从数组中移去从offset开始,长度为length的部份,如果给出了replacement[]参数,则用此参数来取代移去部份。 
对于offset、length的处理判断,与上例相同。 
如果有replacement参数,则用此参数来取代移去部份,如果没有移去,则在offset位置插入。 
以下操作等效: 
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) 
返回一个包含移去元素以后的新数组。 
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) 
从一个数组中移去重复的值。返回新的数组。 
如果是关联数组,key以第一个为准。 
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");. 

以上就介绍了 介绍几个array库的新函数 php,包括了方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn