1. Sorting
1. asort - - Forward sorting, maintain the index relationship
2. arsort - Reverse sorting, maintain the index relationship
3. sort - Sort from lowest to highest
4. ksort - Sort by key name
5. krsort - Reverse sorting by key name
6. rsort - Reverse sorting (highest to lowest), delete the original key name and assign a new key name [letters are higher than numbers]
(1) Pure English: $fruits = array("d" => "lemon", "a" => "orange", "b" => "banana", "c" => "apple");
asort($fruits);//Array ( [c] => apple [b] => banana [d] => lemon [a] => orange )
(2) Pure numbers: $fruits = array("d" => "341", "a" => "524", "b" => "75", "c" => "657");
asort ($fruits);//Array ( [b] => 75 [d] => 341 [a] => 524 [c] => 657 )
(3) Mixing: $fruits = array ("d" => "daf", "a" => "fasd", "b" => "234", "c" => "657");
asort($fruits) ;//Array ( [b] => 234 [c] => 657 [d] => daf [a] => fasd ) Numbers first, then letters "Natural sorting" algorithm sorting
8. natcasesort - Sorting using the "natural sorting" algorithm, case-insensitive
9. usort - Using user-defined comparison functions to sort the values in the array, Delete the original key name
10. uasort - Use user-defined comparison function to sort the values in the array and maintain index association
11. uksort - Use user-defined comparison function to sort the values in the array Sort the key names
12. array_multisort - Sort multiple arrays or multi-dimensional arrays. The associated key names remain unchanged, but the numeric key names will be re-indexed. The first parameter must be an array
13. array_reverse -- Returns an array with the cells in reverse order
$input = array("php", array("green", "red")); $result = array_reverse($input);//Array([0] => Array([0] => green,[1] => red),[1] => php)
$ res = array_reverse($input, TRUE);//true means retaining the original key name
2. key and value
1. key -- Returns the key name of the current cell in the array. 2, Array_key_exists - Check whether the given key name or index is existed in the array, or it can also be used for objects $ Search_array = Array ('first' = & gt; 1, 'second' = & gt. ; 4);
array_key_exists('first', $search_array));//Return TRUE if it exists
does not return TRUE 0 => 100, "color" => "red");
print_r(array_keys($array)); //Array([0] => 0, [1] => color)
array_values‐ array_values($array));//Array([0] => array = array(1, "hello", 1, "world", "hello");
print_r(array_count_values ($array));//Array([1] =>2,[hello] => ;2,[world] => 1)
6. array_flip -- Swap the keys and values in the array. If the same value appears multiple times, only the last one is taken, and the others are lost.
$trans = array("a" => 1, "b" => 1, "c" => 2);
$trans = array_flip($trans);//Array([ 1] => b,[2] => c)
7. array_search -- Search for a given value in the array, and if successful, return the corresponding key name
$array = array(0 => 'blue', 1 => 'red', 2 => 'green', 3 => 'red');
$key = array_search('green', $array); // $key = 2;
$key = array_search('red' , $array); // $key = 1;
8. array_sum -- Calculate the sum of all values in the array
$a = array(2, 4, 6, 8);
echo "sum(a) = ".array_sum($a);//20
array_diff -- Calculate the difference of arrays and return the array. Key names are not used for comparison
$array1 = array("a" => "green", "red", "blue", "red");
$array2 = array("b" => "green", "yellow", "red"); $result = array_diff_assoc($array1, $array2);//Array([1] => blue) 2. array_diff_assoc -- Calculate the difference set of the array with index check, return the array, the key name is also used for comparison, multiple arrays can be compared $array1 = array ("a" => ; "green", "b" => "brown", "c" => "blue", "red"); $array2 = array ("a" => "green", "yellow ", "red");
$result = array_diff_assoc($array1, $array2);//Array([b] =>brown,[c] =>blue,[0] =>red)
. return 0;}}
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result = array_diff_uassoc($array1, $array2, "func");//Array([b ] =>brown,[c] =>blue,[0] =>red) ("a" => "green", "red", "blue");
$array2 = array("b" => "green", "yellow", "red");
$ Result = Array_intersect ($ Array1, $ Array2); // Array ([A] = & GT; Green, [0] = & GT; Red)
5, array_intersect_Assoc Set ,The key name is also used for comparison
$array1 = array("a" => "green", "b" => "brown", "c" => "blue", "red");
$array2 = array("a" => "green", "yellow", "red");
$result_array = array_intersect_assoc($array1, $array2);//Array([a] = > green)
4. Pointer operations
1. current -- Return the current unit in the array
2. reset - - Point the internal pointer of the array to the first unit
3. end -- Point the internal pointer of the array to the last unit
4. next -- Move the internal pointer in the array forward one bit
5. prev -- Rewind the internal pointer of the array by one bit
$transport = array('foot', 'bike', 'car', 'plane');
$mode = current($transport) ; // $mode = 'foot';
$mode = next($transport); // $mode = 'bike';
$mode = next($transport); '; $mode = prev($transport); // $mode = 'bike'; 6. each -- Returns the current key/value pair in the array and moves the array pointer forward one step Return as an array of four cells, with key names 0, 1, key and value. Cells 0 and key contain the key names of the array cells, and 1 and value contain the data. $foo = array("bob", "fred", "jussi", "jouni", "egon", "marliese");
$bar = each($foo);//Array{[1] => bob[value] => bob [0] => 0 [key] => 0}
5. Merge
1. array_merge_recursive -- Merge one or more arrays. If there are the same key names in the array, the latter value will not overwrite the original value. , but appended to the back.
$ar1 = array("color" => array("favorite" => "red"), 5);
$ar2 = array(10, "color" => array("favorite " => "green", "blue"));
$result = array_merge_recursive($ar1, $ar2);
//Array([color] => Array([favorite] => Array([0] =>red,[1] =>green),[0] => blue),[0] => 5,[1] => 10)
2. array_merge -- merge one or more arrays. If the string key name follows the previous one, but the numeric key name is the same, the subsequent value will not overwrite the original value, but will be appended to the end.
$array1 = array("color" => "red", 2, 4);
$array2 = array("a", "b", "color" => "green", " shape" => "trapezoid", 4);
$result = array_merge($array1, $array2);
//Array([color] =>green, [0] =>2, [1] =>4, [2] =>a, [3] =>b, [shape] =>trapezoid, [4] => 4)
. - Create an array, using the value of one array as its key and the value of the other array as its value
$a = array('green', 'red', 'yellow');
$b = array('avocado', 'apple', 'banana');
$c = array_combine($a, $b);//Array([green]=>avocado,[red]=> apple, [yellow] =>banana)
6. Value 1. array_slice(array,offset[,length[,keys]]) - - Take out a section from the array
Offset is non-negative, starting from this offset in the array, if it is negative, it starts from a place so far away from the end in the array
If length is positive, there will be length elements in the array , being negative will end up this far from the end of the array. If omitted, it starts at offset and goes to the end of the array.
Set keys to TRUE to retain the key name, otherwise reset the key name
2. array_rand(input,[num_req]) -- Randomly remove one or more units from the array, num_req specifies How many to take, the default is 1.
$input = array("Neo", "Morpheus", "Trinity", "Cypher", "Tank");
$rand_keys = array_rand($input, 2);
7. Removal and replacement 1. array_splice (array,offset[,length[,replacement]])--remove part of the array and replace it with other values, numbers Key names are not retained
$input = array("red", "green", "blue", "yellow");
array_splice($input, 2);// $input is now array(" Red "," Green ")
$ input = Array (" Red "," Green "," Blue "," YELLOW ");
Array_Splice ($ input, 1, -1);/ / $input is now array("red", "yellow")
$input = array("red", "green", "blue", "yellow");
array_splice($input " blue", "yellow");
array_splice($input, -1, 1, array("black", "maroon"));// $input is now array("red", "green"," blue", "black", "maroon")
, "purple");// $input is now array("red", "green","blue", "purple", "yellow"); The offset specified by the value starts to be removed. If it is negative, the offset specified by the value is counted down from the end and removed
Length: If omitted, all parts of the array from offset to the end are removed. Remove this many units for regularity. If it is negative, remove all units from offset to the reciprocal length of the end of the array
If a replacement array is given, the removed unit will be replaced by the unit in this array
2. array_unique - - Remove duplicate values from the array,
$input = array("a" => "green", "red", "b" => "green", "blue", "red");
$result = array_unique($input);//Array([a] => green,0] => red,[1] => blue)
$result = array_unique($input);//Array([a] => green,0] => red,[1] => blue)
4, "4", "3", 4, 3, "3");
$result = array_unique($input);//array(2) { [0]=> int(4) [2 ]=> string(1) "3" }
3. array_pop -- pop the last unit of the array (pop off the stack), and reduce the length of the array by one. After using this function, the array pointer will be reset.
$stack = array("orange", "banana", "apple", "raspberry");
$fruit = array_pop($stack);
print_r($stack);//Array( [0] => orange,[1] => banana,[2] => apple)
print_r($fruit);//Array([0] => raspberry)
> 4. array_shift -- Move the unit at the beginning of the array out of the array. The numeric key names will be changed to count from zero, and the text key names will remain unchanged
$stack = array("orange", "banana", "apple", " raspberry");
$fruit = array_pop($stack);
print_r($stack);//Array([0] => banana,[1] => apple,[2] => ; raspberry)
print_r($fruit);//Array([0] => orange)
8. Insertion and filling 1. array_pad(input,pad_size,pad_value) -- Fill the array to the specified length with value
If pad_size is positive, the array is filled to the right, if it is negative, it is filled from the left.
$input = array(12, 10, 9);
$result = array_pad($input, 5, 0);// result is array(12, 10, 9, 0, 0)
$result = array_pad($input, -7, -1);// result is array(-1, -1, -1, -1, 12, 10, 9)
$result = array_pad($input, 2, "noop");// not padded
2. array_push -- Push one or more units to the end of the array (push onto the stack)
$stack = array("orange", " banana");
array_push($stack, "apple");//Array([0] => orange,[1] => banana,[2] => apple)
3. array_unshift -- Insert one or more cells at the beginning of the array, the numerical key names are re-counted, and the text key names remain unchanged.
$queue = array("orange", "banana");
array_unshift($queue, "apple");//Array([0] => apple[1] => raspberry[2 ] => orange[3] => banana)
4. array_fill(start_index,num,value)--Fill the array
with the given value num: filled entry start_index: specified key Starting value of name: filled value
$a = array_fill(5, 4, 'banana');//Array([5]=>banana,[6]=>banana,[7]=> ;banana,[8]=>banana)
9. Callback function 1. array_filter -- Use callback function to filter the units in the array
function odd($var){return($var % 2 == 1);}
function even($var){return($var % 2 == 0);}
$array1 = array ("a"=>1, "b"=>2, "c"=>3, "d"=>4, "e"=>5);
$array2 = array( 6, 7, 8, 9, 10, 11, 12);
print_r(array_filter($array1, "odd")); //Array([a] =>1,[c] = >3,[e] => 5)
print_r(array_filter($array2, "even")); //Array([0] =>6,[2] =>8,[4 ] =>10,[6] =>12)
2. array_map -- Apply the callback function to the cells of the given array
function cube($n){return($n * $n * $n);}
$a = array(1, 2, 3, 4, 5); $b = array_map("cube" , $a);//Array([0] =>1,[1] =>8,[2] =>27,[3] =>64,[4] =>125)
array_reduce‐ function rmul($v, $w){$v *= $w;return $v;}
$a = array(1, 2, 3, 4, 5);
$x = array() ;
$b = array_reduce($a, "rsum"); //15=1+2=3+4+5
$c = array_reduce($a, "rmul", 10);// 1200 (= 10*1*2*3*4*5)
$d = array_reduce($x, "rsum", 1);//1
10. Others
1. array_chunk -- Split an array into multiple $input_array = array('a', 'b', 'c', 'd', 'e') ; print_r(array_chunk($input_array, 2)); //The default value is FALSE, each result array will be indexed with a new number starting from zero print_r(array_chunk($input_array, 2, true)) ;//TRUE indicates the original key name in the array
2. shuffle -- shuffle (randomly arrange the order of cells) an array. Delete the original key name and assign a new key name
3. in_array -- Check whether a certain value exists in the array, case sensitive
4. Array and json swap
json_encode() converts a PHP array into Json.
json_decode() is to convert Json into a PHP array
What is $array[] in php? $array is an array
That’s it,
To delete it, use unset($array[1]);
http://www.bkjia.com/PHPjc/834017.html
www.bkjia.com