Php basic knowledge Summary (1) array, basic knowledge array

Source: Internet
Author: User
Tags php array to json

Php basic knowledge Summary (1) array, basic knowledge array

I. Sorting
1. asort-Positive Sorting to maintain the index relationship
2. arsort-reverse sorting to maintain the index relationship
3. sort -- sorting from the lowest to the highest
4. ksort -- sort by key name
5. krsort-sort by key name in reverse order
6. rsort-reverse sort (the highest to the lowest), delete the original key name, and assign the new key name [higher than the number]
(1) English only: $ fruits = array ("d" => "lemon", "a" => "orange", "B" => "banana ", "c" => "apple ");
Asort ($ fruits); // Array ([c] => apple [B] => banana [d] => lemon [a] => orange)
(2) pure number: $ fruits = array ("d" => "341", "a" => "524", "B" => "75 ", "c" => "657 ");
Asort ($ fruits); // Array ([B] => 75 [d] => 341 [a] => 524 [c] => 657)
(3) HYBRID: $ fruits = array ("d" => "daf", "a" => "fasd", "B" => "234 ", "c" => "657 ");
Asort ($ fruits); // Array ([B] => 234 [c] => 657 [d] => daf [a] => fasd) numbers and letters

7. natsort-sort by "natural sorting" Algorithm
8. natcasesort-sort by natural sorting algorithm, case insensitive
9. usort -- Sort the values in the array using the User-Defined comparison function and delete the original key name
10. uasort-sort the values in the array using the User-Defined comparison function and maintain the index Association
11. uksort -- Sort the key names in the array using the User-Defined comparison Function

12. array_multisort -- sorts multiple arrays or multi-dimensional arrays. The association key name remains unchanged, but the number key name is re-indexed. The first parameter must be an array.
13. array_reverse -- returns an array with the opposite unit 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 indicates that the original key name is retained.


II. key and value
1. key -- return the key name of the current unit in the array.

2. array_key_exists -- check whether the given key name or index exists in the array. It can also be used for objects.
$ Search_array = array ('first' => 1, 'second' => 4 );
Array_key_exists ('first', $ search_array); // returns TRUE if the object exists.

3. array_keys -- returns all the key names in the array.
$ Array = array (0 => 100, "color" => "red ");
Print_r (array_keys ($ array); // Array ([0] => 0, [1] => color)

4. array_values -- returns all values in the array.
$ Array = array ("size" => "XL", "color" => "gold ");
Print_r (array_values ($ array); // Array ([0] => XL [1] => gold)

5. array_count_values -- count the number of occurrences of all values in the array
$ 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 all 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. If the value is successful, the corresponding key name is returned.
$ 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


3. intersection and difference set
1. array_diff -- calculates the difference set of the array and returns an array. The key name is 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 the index check. The array is returned and 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)

3. array_diff_uassoc -- use the callback function provided by the user to perform index check to calculate the array difference set
Function func ($ a, $ B) {if ($ a ===$ B) {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)

4. array_intersect: calculates the intersection of arrays. The key name is not used for comparison.
$ Array1 = array ("a" => "green", "red", "blue ");
$ Array2 = array ("B" => "green", "yellow", "red ");
$ Result = array_intersect ($ array1, $ array2); // Array ([a] => green, [0] => red)

5. array_intersect_assoc -- intersection of arrays computed by index check. 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)


Iv. 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 to a forward position
5. prev -- returns the internal pointer of the array to one
$ Transport = array ('foot', 'bike', 'car', 'plane ');
$ Mode = current ($ transport); // $ mode = 'foot ';
$ Mode = next ($ transport); // $ mode = 'bike ';
$ Mode = next ($ transport); // $ mode = 'car ';
$ Mode = prev ($ transport); // $ mode = 'bike ';
6. each -- return the current key/value pair in the array and move the array pointer one step forward.
Returns an array of four units, with the key names 0, 1, key, and value. Unit 0 and key contain the key name of the array unit, and 1 and value contain data.
$ Foo = array ("bob", "fred", "jussi", "jouni", "egon", "marliese ");
$ Bar = each ($ foo); // Array {[1] => bob [value] => bob [0] => 0 [key] => 0}


V. Merge
1. array_merge_recursive -- combines one or more keys. If the array has the same key name, the last value will not overwrite the original value, but will be 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 -- merges one or more arrays. If the string key name is followed by the previous one, but the numeric key name is the same, the subsequent values will not overwrite the original values, instead, it is appended to the backend.
$ 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)

3. array_combine -- creates an array with the value of one array as its key name and the value of the other array as its value
$ A = array ('green', 'red', 'yellow ');
$ B = array ('avcado', 'apple', 'bana ');
$ C = array_combine ($ a, $ B); // Array ([green] => avocado, [red] => apple, [yellow] => banana)

Vi. Values
1. array_slice (array, offset [, length [, keys]) -- retrieves a segment from the array
Offset is not negative. It starts from this offset in the array and is negative. It starts from the end of the array so far away.
Length is positive, and the array will have length elements. If it is negative, it will end so far away from the end of the array. If this parameter is omitted, it starts from offset to the end of the array.
Keys is set to TRUE to retain the key name; otherwise, the key name is reset.


2. array_rand (input, [num_req]) -- randomly retrieves one or more units from the array. num_req indicates the number of units to be retrieved. The default value is 1.
$ Input = array ("Neo", "Morpheus", "Trinity", "Cypher", "Tank ");
$ Rand_keys = array_rand ($ input, 2 );

VII. De-value and substitution
1. array_splice (array, offset [, length [, replacement]) -- removes a part of the array and replaces it with other values. The number key name is 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, 1, count ($ input), "orange"); // $ input is now array ("red", "orange ")

$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input,-1, 1, array ("black", "maroon"); // $ input is now array ("red", "green ", "blue", "black", "maroon ")

$ Input = array ("red", "green", "blue", "yellow ");
Array_splice ($ input, 3, 0, "purple"); // $ input is now array ("red", "green", "blue", "purple ", "yellow ");

Offset: positive. It is removed from the offset specified by this value in the array. If it is negative, it is removed from the offset specified by the reciprocal value at the end.
Length: If this parameter is omitted, all parts from the offset to the end of the array are removed. Remove so many cells from the regular expression. If it is negative, all units in the center from offset to the reciprocal length at the end of the array are removed.
If the replacement array is provided, the removed unit is replaced by the Unit in the array.

2. array_unique -- remove repeated values from the array,
$ Input = array ("a" => "green", "red", "B" => "green", "blue", "red ");
$ Result = array_unique ($ input); // Array ([a] => green, 0] => red, [1] => blue)

$ Input = array (4, "4", "3", 4, 3, "3 ");
$ Result = array_unique ($ input); // array (2) {[0] => int (4) [2] => string (1) "3 "}

3. array_pop -- pops up the last unit of the array (output stack). The length of the array is reduced by one. After using this function, the array pointer is 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 -- removes the unit starting with the array from the array. The number key name is changed to count from scratch, and the text key name remains 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. insert and fill
1. array_pad (input, pad_size, pad_value) -- fill the array with a value to the specified length
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 (in the stack)
$ Stack = array ("orange", "banana ");
Array_push ($ stack, "apple"); // Array ([0] => orange, [1] => banana, [2] => apple)

3. array_unshift: insert one or more units at the beginning of the array. The value key name is re-counted, and the text key name remains 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: Enter start_index: Specifies the start value of the key name: The filled value.
$ A = array_fill (5, 4, 'Banana '); // Array ([5] => banana, [6] => banana, [7] => banana, [8] => banana)

IX. Callback Functions
1. array_filter -- use the callback function to filter elements 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 -- calls the callback function to the unit of the given array.
Function cube ($ n) {return ($ n * $ n );}
$ A = array (1, 2, 3, 4, 5 );
$ B = array_map ("cube", $ a); // Array ([0] => 1, [1] => 8, [2] => 27, [2] => 64, [4] => 125)

3. array_reduce -- use the callback function to iteratively simplify the array to a single value
Function rsum ($ v, $ w) {$ v + = $ w; return $ v ;}
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 -- splits 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 is indexed with a new number starting from scratch.
Print_r (array_chunk ($ input_array, 2, true); // TRUE indicates the original key name in the array

2. shuffle-disrupt an array (the order of randomly arranged units. Delete the original key name and assign the new key name

3. in_array -- check whether a value exists in the array, case sensitive

4. array and json Interchange
Json_encode () is to convert the PHP array to Json.
Json_decode () is to convert Json into a PHP array.


PHP defines an array as follows: $ arr = array (); what is the content of this array?

This array is empty. You can use print_r (); to check the result:
Print_r ($ arr );

What is $ array [] in php? $ Array is an array

I don't quite understand what it means. $ array is an array. Why?
Value assignment
$ Array [] = "1 ";
$ Array [] = "2 ";
In this way,
Unset ($ array [1]);

Related Article

E-Commerce Solutions

Leverage the same tools powering the Alibaba Ecosystem

Learn more >

Apsara Conference 2019

The Rise of Data Intelligence, September 25th - 27th, Hangzhou, China

Learn more >

Alibaba Cloud Free Trial

Learn and experience the power of Alibaba Cloud with a free trial worth $300-1200 USD

Learn more >

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.