Introducing several new functions of the array library php_PHP tutorial

WBOY
Release: 2016-07-21 15:58:46
Original
826 people have browsed it

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

Let me introduce them to you now. 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 merge the contents of array2 Added 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).


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

例: 
$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 ...]) 

与上个函数相反,这是取多个数组的差集了。 

例: 
$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) 

这两个函数有关系,放在一起了。 
array_keys可以取出数组所有的key,如果定义了search_value,就只取相应的key值。 
array_values取出数组input的所有value值。 

例: 

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

对多个数组同时进行排序,或是对一个多维数组进行多个维的排序。(很有用哦,我上回在中文用户就问了这个问题)。 

输入的数组被处理成表的列,按行来排序,有点类似于sql语句中的order by条件。 
这个函数的参数不常见,但是很灵活。可是一个数组或是下面这几个标志。 

SORT_ASC - 升序 

SORT_DESC - 降序 

SORT_REGULAR - 常规比较 

SORT_NUMERIC - 数值比较 

SORT_STRING - 字串比较 


一个数组不可以同时给两种类型的排序标志(这个当然了)。每个数组后的标志只对此数组有效。缺省为 SORT_ASC 和 SORT_REGULAR 。 

如果正常,返回true,否则返回false。 

例1: 
$ar1 = array ("10", 100, 100, "a"); 
$ar2 = array (1, 3, "2", 1); 
array_multisort ($ar1, $ar2); 

//结果是 $ar1 = 10, "a", 100, 100. $ar2= 1, 1, 2, "3". 

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

不过,上面这个例子我试了一下,不行,会报参数3要求是数组的错误。(???这个俺也不知了) 

如果你直接用 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 ...])

A function used to use 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])

Randomly select one or more elements from the array. The parameter num_req gives the number of elements to be selected, and the default is 1.
Returns an array whose content is the key of the selected element.

You must 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, putting the input elements 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])
Take a part of an array, starting from offset, with a length of length, and the default is to the end.
Returns a new array.

If offset is positive, it starts from the offset position of the array. If it is negative, it counts from the end of the array.
If length is positive, it is the length of the new array, if it is negative, it is also counting down 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]])

From Remove the part starting from offset and length length from the array. If the replacement[] parameter is given, this parameter will be used to replace the removed part.

The processing and judgment of offset and length are the same as the above example.
If there is a replacement parameter, this parameter will be used to replace the removed part. If it is not removed, it will be 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 an array containing shift The new array after removing the 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)

Remove duplicate values ​​from an array. Return the new array.
If it is an associative array, the key shall be the first one.

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

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/317537.htmlTechArticleWe don’t have much PHP information at hand. Does everyone have a copy of php4gb.chm. What I appreciate most is the function library part, the real online help. But the pace of PHP development is too fast,...
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!