PHP common Functions-array php Common String functions php common class library PHP common English list

Source: Internet
Author: User
Tags array sort shuffle
The process of learning PHP, sorting out some of the commonly used functions, which are array functions.

Array (): Generate an array
$a = Array ("Dog", "cat", "horse");
Print_r ($a); Array ([0] = dog [1] = cat [2] = = horse)
Array_combine (): Generates an array with the value of an array as the key name, and the value of the other array as the value
$a 1 = Array ("A", "B", "C", "D");
$a 2 = Array ("Cat", "Dog", "Horse", "Cow");
Print_r (Array_combine ($a 1, $a 2)); Array ([A] = Cat [b] = Dog [c] = Horse [d] = Cow)
Range (): Creates and returns an array containing the elements of 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 of each time)
Print_r ($number); Array ([0] = 0 [1] = [2] = [3] = [4] = [5] = 50)
Compact (): Create an array of variables with parameters
$firstname = "Peter";
$lastname = "Griffin";
$age = "38";
$result = Compact ("FirstName", "LastName", "age");
Print_r ($result); Array ([FirstName] = Peter [LastName] = Griffin [age] + 38)
Array_fill (): Generates an array with the given value
$a = Array_fill (2,3, "Dog"); (2: The first key value of the fill, 3: The value of the fill; Dog: The contents of the fill)
Print_r ($a); Array ([2] = dog [3] = dog [4] = dog)
Array_chunk (): Splits an array into a new array block
$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 (): Merging two arrays into an array
The difference between/*********************** and Array_combine ***********************
Array_merge (): merge arrays directly; Array_combine (): The first group is the key according to the parameter order, the second group is the value; */
echo "

";
$a 1 = Array ("A" = "Horse", "b" = "Dog");
$a 2 = Array ("c" = "Cow", "B" = "cat");
Print_r (Array_merge ($a 1, $a 2)); Array ([A] = Horse [b] = Dog [c] = = Cow [d] = + cat)
Array_diff (): Returns the difference set of two arrays (the key name remains the same)
$a 1 = Array (8=> "Cat",1=> "Dog",2=> "Horse",3=> "lion");
$a 2 = Array (4=> "Horse",5=> "Dog",6=> "Bird",7=> "pig");
Print_r (Array_diff ($a 1, $a 2)); Array ([8] = Cat [3] = Lion)
Print_r (Array_diff ($a 2, $a 1)); Array ([6] = bird [7] = pig)
Array_intersect (): Returns an array of intersections of two or more arrays
$a 1 = Array (0=> "Cat",1=> "Dog",2=> "Horse");
$a 2 = Array (3=> "Horse",4=> "Dog",5=> "Fish");
Print_r (Array_intersect ($a 1, $a 2)); Array ([1] = Dog [2] = Horse)
Print_r (Array_intersect ($a 2, $a 1)); Array ([3] = Horse [4] = Dog)
Array_serach searches for the given value in the array and returns the corresponding key name if successful (false return failure)
$a = Array ("A" = "Dog", "b" = "Cat", "c" = "Horse");
Echo Array_search ("Dog", $a); A
Array_slice (): Takes a value out of a condition in the array and returns (the key name remains unchanged)
echo "
";
$a = Array ("A" = "Dog", "b" = "Cat", "c" = "Horse", "D" = "Bird");
Print_r (Array_slice ($a,)); 1: Starting from the key value (equivalent to 1 of the index key); 2, take Two
Array ([b] = Cat [c] = Horse)
Array_splice (): Remove part of the array and replace it with other values
$a 1 = Array (4=> "Dog", ' b ' = ' Cat ', ' c ' = ' Horse ',6=> "Bird");
$a 2 = Array (3=> "Tiger",5=> "Lion");
Array_splice ($a 1,1,2, $a 2);
/* $a 1: Replaced array (last output array), 1: Replace by 1 position of index key, 2: Replace two, $a 2: Replace array, add to $a1 */
Print_r ($a 1); Array ([0] = Dog [1] = Tiger [2] = Lion [3] = = Bird)
Array_splice ($a 1,0,2, $a 2);
Print_r ($a 1); Array ([0] = Tiger [1] = Lion [2] = = Horse [3] = = Bird)
Array_sum (): Computes the and of all values in the array
$a = Array (0=> "5",1=> ",2=>" "25");
echo array_sum ($a); 45
In_array (): Checks if a 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 (): Checks if the given key name exists in the array (parameter 1: Key Name parameter 2: Array): returns 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 current pointer to the element inside the array
Current (): Returns the elements of the array
Next (): Moves the pointer to the current element to the position of the next element and returns the value of the current element
Prev (): Moves the pointer to the current element 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 first value and returns the value of the 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
/* Iterate array */
/* Forward direction traversal */
$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 ');
/* Default First is current element */
$mode = current ($transport); $mode = ' foot ';
$mode = Next ($transport); $mode = ' bike ';
/* 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 (): Assigns 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 (): Deletes the first element in the array and returns 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 (): Inserts one or more elements in the array switch (starting from 0 if the current array is an indexed array, and so on; Associative array key names are unchanged)
$a = Array ("Ten" = "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 (): One or more elements are pressed into the array at the end
$a =array ("a" and "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 (): The array is scrambled, the key name is indexed array starting from 0 (cannot print directly shuffle, separate write)
$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) Every refresh will change randomly
COUNT (): Counts the number of properties in an array of cells in an object
$people = Array ("Peter", "Joe", "Glenn", "Cleveland");
echo count ($people); 4
Array_flip (): Returns an array after a key-value reversal
$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 an array, forming 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 the values in the array, 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 of elements in 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 (): Counts 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 extracts one or more elements from an array, note the key name
$a =array ("a" and "Dog", "B" and "Cat", "c" = "Horse", "D" and "Lion", "e" = "Cow");
Print_r (Array_rand ($a, 3)); Array ([0] = = b [1] = c [2] + E) * * * random * * *
Each (): Returns the current key/value pair in the array and moves the array pointer backward one step
$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, the pointer moves backwards one */
$bar = each ($foo);
Print_r ($bar); Array ([1] = fred [value] = Fred [0] = 1 [key] = 1)
Array_unique (): Delete duplicate value, return remaining array
$a =array ("a" and "dog", "b" = "Cat", "c" = "Horse", "D" and "dog", "e" = "cow", "f" and "cow");
Print_r (Array_unique ($a)); Array ([A] = Dog [b] + Cat [c] = = Horse [E] = cow [f] = cow)
/* Array Sort */
/**
* Return value is 1 (positive): Indicates the Exchange order
* Return value is-1 (negative): Indicates no exchange order
**/
/**
*//original key name is ignored (zero-based) (string order)
* Sort (): For values from small to large
* Rsort (): For values from large to small
*
*//original Key name reserved (string order)
* Asort (): For values from small to large
* Arsort (): For 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 (): For subscript from small to large
* Krsort (): for subscript from big 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");//Sort by what order
Ksort ($my _array);
Print_r ($my _array); Array ([A] = Horse [E] = Dog [2] = Cat)
/**
* Usort (): Sort arrays Using user-defined callback functions
* Uasort (): Use a user-defined callback function to sort the array and keep the index associated
* Uksort (): Sort array key by using user-defined callback function
**/
$v = Array (1,3,5,2,4);
Usort ($v, ' fun ');
function Fun ($v 1, $v 2) {
Return ($v 1 > $v 2)? 1:-1;
}
Print_r ($v); Array ([0] = 1 [1] = 2 [2] = 3 [3] = 4 [4] = 5)
/* Sort plus traverse */
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 traverse end */
/**
* Sort (): sorted by small to large string (equal letters, one size after unequal)
* Natsort (); natural sort from small to large (letters equal, comparison 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)

The above describes the PHP commonly used functions-arrays, including PHP, common aspects of the content, I hope that the PHP tutorial interested in a friend helpful.

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