PHP commonly used functions - array PHP commonly used string functions PHP commonly used class libraries PHP commonly used English singles

WBOY
Release: 2016-07-29 08:50:42
Original
1024 people have browsed it

In the process of learning PHP, I compiled some commonly used functions. These are array functions.

//array(): Generate an array
$a = array("dog","cat","horse");
print_r($a); //Array ( [0] = > dog [1] => cat [2] => horse )
//array_combine(): Generate an array, using the value of one array as the key name and the value of the other array as the value
$a1 = array ("a","b","c","d");
$a2 = array("Cat","Dog","Horse","Cow");
print_r(array_combine($a1,$ a2)); //Array ( [a] => Cat [b] => Dog [c] => Horse [d] => Cow )
//range(): Create and return an array containing elements in the specified range.
$number = range(0,50,10); //(0: the first value of the sequence; 50: the end value of the sequence; 10: the step size each time)
print_r ($number); //Array ( [0] => 0 [1] => 10 [2] => 20 [3] => 30 [4] => 40 [5] => 50 )
//compact(): Create an array composed of variables carried by parameters
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = compact("firstname" , "lastname", "age");
print_r($result); //Array ( [firstname] => Peter [lastname] => Griffin [age] => 38 )
//array_fill(): Generate an array with the given value
$a = array_fill(2,3,"Dog"); //(2: The first key value to be filled; 3: The value to be filled; dog: fill in the content)
print_r($a); //Array ( [2] => Dog [3] => Dog [4] => Dog )
//array_chunk(): put an array Split into new array blocks
$a = array("a"=>"cat","b"=>"dog","c"=>"horse","d"=>"Cow ");
print_r(array_chunk($a,2)); //Array([0] => Array([a]=>cat [b]=>dog) [1] => Array( [c]=>horse [d]=>cow))
//array_merge(): Merge two arrays into one array
/***********************Differences between array_combine******************************
array_merge( ): Merge arrays directly; array_combine(): According to the order of parameters, the first group is the key and the second group is the value;*/
echo "


" ;
$a1 = array("a"=>"Horse","b"=>"Dog");
$a2 = array("c"=>"Cow","b"=> "cat");
print_r(array_merge($a1,$a2)); //Array ( [a] => Horse [b] => Dog [c] => Cow [d] => cat )
//array_diff(): Returns the difference between the two arrays (the key names remain unchanged)
$a1 = array(8=>"Cat",1=>"Dog",2=>"Horse" ,3=>"lion");
$a2 = array(4=>"Horse",5=>"Dog",6=>"bird",7=>"pig");
print_r(array_diff($a1,$a2)); //Array ( [8] => Cat [3] => lion )
print_r(array_diff($a2,$a1)); //Array ( [6 ] => bird [7] => pig )
//array_intersect(): Returns the intersection array of two or more arrays
$a1 = array(0=>"Cat",1=>"Dog ",2=>"Horse");
$a2 = array(3=>"Horse",4=>"Dog",5=>"Fish");
print_r(array_intersect($a1, $a2)); // Array ( [1] => Dog [2] => Horse )
print_r(array_intersect($a2,$a1)); // Array ( [3] => Horse [4 ] => Dog )
//array_serach searches for the given value in the array, and returns the corresponding key name if successful (returns false on failure)
$a = array("a"=>"Dog","b "=>"Cat","c"=>"Horse");
echo array_search("Dog",$a); //a
//array_slice(): Remove a value from the array based on conditions, And return (the key name remains unchanged)
echo "
";
$a = array("a"=>"Dog","b"=>"Cat","c"= >"Horse","d"=>"Bird");
print_r(array_slice($a,1,2)); //1: Start from the key value (equivalent to the position where the index key is 1) ;2. Take two
//Array ( [b] => Cat [c] => Horse )
//array_splice(): Remove part of the array and replace it with other values
$a1 = array (4=>"Dog",'b'=>"Cat",'c'=>"Horse",6=>"Bird");
$a2 = array(3=>"Tiger ",5=>"Lion");
array_splice($a1,1,2,$a2);
/* $a1: The replaced array (the last array output); 1: 1 according to the index key Start replacing at the position; 2: Replace two; $a2: Replace the array and add it to $a1*/
print_r($a1); //Array ( [0] => Dog [1] => Tiger [ 2] => Lion [3] => Bird )
//array_splice($a1,0,2,$a2);
//print_r($a1); // Array ( [0] => Tiger [1] => Lion [2] => Horse [3] => Bird )
//array_sum(): Calculate the sum of all values in the array
$a = array(0=>"5", 1=>"15",2=>"25");
echo array_sum($a); //45
//in_array(): Check whether a certain value exists in the array
$animals = array(" dog", "cat", "cow", "horse");
if (in_array("cow",$animals)){
echo "Match found";
}else{
echo "Match not found";
}
//array_key_exists(): Check whether the given key name exists in the array (parameter 1: key name parameter 2: array): return bool value
$animals = array("a"=>"dog", " b"=>"cat", "c"=>"cow", "d"=>"horse","d"=>"lion");
echo array_key_exists("a",$animals ); //1 does not return false value
$search_array = array('first' => 1, 'second' => 4);
if (array_key_exists('first', $search_array)) {
echo " The 'first' element is in the array";
} //The 'first' element is in the array
/* Array pointer operation*/
//key(): Returns the key name of the element currently pointed to by the array's internal pointer
//current(): Returns the current element of the array
//next(): Moves the pointer pointing to the current element to the position of the next element, and returns the value of the current element
//prev(): Moves the pointer pointing to the current element The pointer moves to the position of the previous element and returns the value of the current element
//end(): Points the current internal pointer to the last element and returns the value of the element
//reset(): Points the array element pointer to the A value and returns the value of this element
$array = array(
'fruit1' => 'apple',
'fruit2' => 'orange',
'fruit3' => 'grape',
'fruit4' => 'apple',
'fruit5' => 'apple');
while ($fruit_name = current($array)) {
if ($fruit_name == 'apple') {
echo key ($array).'
';
}
next($array);
} //fruit1 fruit4 fruit5
/* Traverse the array*/
/*Traverse in the forward direction*/
$a = array (10,20,30);
reset($a);
do{
echo key($a)."==>".current($a)."
";
} while(next($a)); // 0==>10 1==>20 2==>30
/*Backward traversal*/
end($a);
do{
echo key ($a)."===>".current($a)."
";
}while(prev($a)); //2===>30 1===>20 0===>10
/* pointer*/
$transport = array('foot', 'bike ', 'car', 'plane');
/*The first one is the current element by default*/
$mode = current($transport); // $mode = 'foot';
$mode = next($transport ); // $mode = 'bike';
/*The current element is 'bike'*/
$mode = current($transport); // $mode = 'bike';
$mode = prev($transport) ; // $mode = 'foot';
$mode = end($transport); // $mode = 'plane';
$mode = current($transport); // $mode = 'plane';
/ /list(): Assign the values in the array to some variables---------list is not a function
$arr = array("Cat","Dog","Horse","Cow");
list($a,$b,$c,$d) = $arr;
echo $a; //Cat
echo $b; //Dog
echo $c; //Horse
echo $d; //Cow
//array_shift(): Delete the first element in the array and return the value of the deleted element
$a = array("1"=>"Dog","2"=>"Cat"," 3"=>"Horse");
echo array_shift($a); //Dog
print_r ($a); //Array ( [b] => Cat [c] => Horse )
// array_unshift(): Insert one or more elements into the array switch (if the current array is an index array, it starts from 0, and so on; the associative array key name remains unchanged)
$a = array("10"=>" Cat","b"=>"Dog",3=>"horse",5=>"lion");
array_unshift($a,"Horse");
print_r($a); // Array ( [0] => Horse [1] => Cat [b] => Dog [2] => horse [3] => lion )
//array_push(): Push one or more elements into the array at the end
$a=array("a"=>"Dog","3"=>"Cat");
array_push($a, "Horse","Bird");
print_r($a); //Array ( [a] => Dog [3] => Cat [4] => Horse [5] => Bird )
//array_pop(): Delete the last element in the array
$a=array("Dog","Cat","Horse");
array_pop($a);
print_r($a); //Array ( [0] => Dog [1] => Cat )
/* Array key value operation */
//shuffle(): shuffle the array, the key name index array starts from 0 (shuffle cannot be printed directly, Write separately)
$animals = array("a"=>"dog", "b"=>"cat", "c"=>"cow", "d"=>"horse"," d"=>"lion");
shuffle($animals);
print_r($animals); //Array ( [0] => dog [1] => cow [2] => cat [ 3] => lion ) will change randomly every time it is refreshed
//count(): Calculate the number of units in the array and the number of attributes in the object
$people = array("Peter", "Joe", "Glenn" , "Cleveland");
echo count($people); //4
//array_flip(): Returns an array with the key values reversed
$a = array(0=>"Dog",1=> ;"Cat",2=>"Horse");
print_r(array_flip($a)); //Array ( [Dog] => 0 [Cat] => 1 [Horse] => 2 )
//array_keys(): Returns all the keys of the array to form an array
$a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog ");
print_r(array_keys($a)); //Array ( [0] => a [1] => b [2] => c )
//array_values(): Returns all items in the array values, forming an array
$a = array("a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_values($a )); //Array ( [0] => Horse [1] => Cat [2] => Dog )
//array_reverse(): Returns an array with the elements in the reverse order
$a = array(" a"=>"Horse","b"=>"Cat","c"=>"Dog");
print_r(array_reverse($a)); //Array ( [c] => Dog [b] => Cat [a] => Horse )
//array_count_values(): Count the number of occurrences of all values in the array
$a = array(1,2,3,4,1,1,3 ,5,3,2,1,3,4);
print_r(array_count_values($a)); //Array ( [1] => 4 [2] => 2 [3] => 4 [ 4] => 2 [5] => 1 )
//array_rand(): Randomly extract one or more elements from the array, note the key name
$a=array("a"=>"Dog ","b"=>"Cat","c"=>"Horse","d"=>"Lion","e"=>"Cow");
print_r(array_rand($a ,3)); //Array ( [0] => b [1] => c [2] => e ) ***Random***
//each(): Returns the current item in the array key/value pairs and move the array pointer one step backward
$foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($ foo);
print_r($bar); //Array ( [1] => bob [value] => bob [0] => 0 [key] => 0 )
/* Each time it is traversed, Move the pointer one position backward*/
$bar = each($foo);
print_r($bar); //Array ( [1] => fred [value] => fred [0] => 1 [key] => 1 )
//array_unique():删除重复值,返回剩余数组
$a=array("a"=>"Dog","b"=>"Cat","c"=>"Horse","d"=>"Dog","e"=>"cow","f"=>"Cow");
print_r(array_unique($a)); //Array ( [a] => Dog [b] => Cat [c] => Horse [e] => cow [f] => Cow )
/* 数组排序 */
/**
* The return value is 1 (positive value): indicates exchange order
* The return value is -1 (negative value): indicates no exchange order
**/
/**
* //The original key name is ignored (starting from zero) (string order)
* sort(): Values from small to large
* rsort(): Values from large to small
*
* //Original Key name preservation (string order)
* asort(): sort the values from small to large
* arsort(): sort the values from large to small
**/
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
sort($my_array);
print_r($my_array); //Array ( [0] => Cat [1] => Dog [2] => Horse )
$my_array = array("a" => "Dog", "b" => "Cat", "c" => "Horse");
asort($my_array);
print_r($my_array); //Array ( [b] => Cat [a] => Dog [c] => Horse )
/**
* ksort(): Sort the subscripts from small to large
* krsort(): Sort the subscripts from large to small
**/
$my_array = array("h" => "Dog", "s" => "Cat", "a" => "Horse");
ksort($my_array);
print_r($my_array); //Array ( [a] => Horse [h] => Dog [s] => Cat )
$my_array = array("e" => "Dog", "2" => "Cat", "a" => "Horse");//按什么顺序排序
ksort($my_array);
print_r($my_array); //Array ( [a] => Horse [e] => Dog [2] => Cat )
/**
* usort(): Use user-defined callback function to sort the array
* uasort(): Use user-defined callback function to sort the array and maintain index association
* uksort(): Use user-defined callback function to sort the array Array sort sorts array keys
**/
$v = array(1,3,5,2,4);
usort($v,'fun');
function fun($v1,$v2){
return ( $v1 > $v2 ) ? 1 : -1;
}
print_r($v); //Array ( [0] => 1 [1] => 2 [2] => 3 [3] => 4 [4] => 5 )
/* 排序加遍历 */
function cmp($a, $b)
{
if ($a == $b) {
return 0;
}
return ($a < $b) ? -1 : 1;
}
$a = array(3, 2, 5, 6, 1);
usort($a, "cmp");
foreach ($a as $key => $value) {
echo $key."===>".$value." "; //0===>1 1===>2 2===>3 3===>5 4===>6
}
/* 排序遍历结束 */
/**
* sort(): Sort strings from small to large (letters are equal, compare the one digit after unequal)
* natsort(); Natural sort from small to large (letters are equal, compare values)*** Case sensitive
* natcasesort(): Case-insensitive natural sorting
**/
$a = array("a" => "id2", "b" => "id12", "c" => "id22","d" => "ID22");
sort($a); print_r($a); //Array ( [0] => ID22 [1] => id12 [2] => id2 [3] => id22 )
natsort($a); print_r($a); //Array ( [0] => ID22 [2] => id2 [1] => id12 [3] => id22 )
natcasesort($a); print_r($a); //Array ( [2] => id2 [1] => id12 [3] => id22 [0] => ID22 )

以上就介绍了php常用函数-数组,包括了php,常用方面的内容,希望对PHP教程有兴趣的朋友有所帮助。

Related labels:
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
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!