PHP Basics Summary (1) array, basic knowledge array_php Tutorial

Source: Internet
Author: User
Tags php basics

PHP Basic Knowledge Summary (1) array of arrays, basics array


First, sort
1, Asort--positive sort, keep index relation
2, Arsort--reverse sort, keep index relationship
3. Sort-from lowest to highest
4. Ksort-Sort by key name
5. Krsort--Reverse Sort by key name
6, Rsort-reverse sort (highest to lowest), delete the original key name, give the new key name "letter higher than the number"
(1) Plain English: $fruits = Array ("d" = "lemon", "a" and "Orange", "b" = "banana", "C" and "Apple");
Asort ($fruits);//array ([c] = Apple [b] = banana [d] = = lemon [a] = orange)
(2) Pure numbers: $fruits = Array ("D" = "341", "a" and "524", "b" = "", "c" = "657");
Asort ($fruits);//array ([b] = [d] = 341 [a] = = 524 [c] + = 657)
(3) Mixing: $fruits = Array ("D" = "DAF", "a" = "FASD", "b" = "234", "C" and "657");
Asort ($fruits);//array ([b] = 234 [c] = 657 [d] = DAF [A] = FASD) First digit after letter

7. Natsort-Sort by "natural sort" algorithm
8, Natcasesort-sort with "natural sort" algorithm, case-insensitive
9, Usort--Use the user-defined comparison function to sort the values in the array, delete the original key name
10, Uasort--use a user-defined comparison function to sort the values in the array and keep the index associated
11, Uksort--using a user-defined comparison function to sort the key names in the array

12. Array_multisort-Sorts multiple arrays or multidimensional arrays, the associated key name remains the same, but the numeric key name is re-indexed. The first argument must be an array
13, Array_reverse-Returns an array of 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 preserving the original key name


second, 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 exists in the array, and can also be used for the object
$search _array = Array (' First ' = 1, ' second ' + 4);
Array_key_exists (' first ', $search _array));//returns TRUE when present

3, Array_keys--Returns all the key names in the array
$array = Array (0 = +, "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--the number of occurrences of all values in the statistical 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, take only the last one, the others are lost.
$trans = Array ("a" = = 1, "b" = 1, "c" = 2);
$trans = Array_flip ($trans);//array ([1] = b,[2] = c)

7, Array_search--searches the array for the given value and returns the corresponding key name if successful
$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-Calculates the and of all values in the array
$a = Array (2, 4, 6, 8);
echo "sum (a) =". Array_sum ($a);//20


three, intersection and difference set
1, Array_diff--Calculates the difference set of the array, returns the array, the key name is not used for comparison
$array 1 = Array ("A" = "green", "Red", "Blue", "Red");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_diff_assoc ($array 1, $array 2);//array ([1] = blue)

2, ARRAY_DIFF_ASSOC--with index check the difference set of the computed array, return the array, the key name is also used for comparison, can be more than a group comparison
$array 1 = Array ("A" = "green", "b" = "Brown", "c" = "blue", "Red");
$array 2 = Array ("A" = "green", "yellow", "red");
$result = Array_diff_assoc ($array 1, $array 2);//array ([b] =>brown,[c] =>blue,[0] =>red)

3, ARRAY_DIFF_UASSOC--use the user-provided callback function to do index check to calculate the difference set of the array
function func ($a, $b) {if ($a = = = $b) {return 0;}}
$array 1 = Array ("A" = "green", "b" = "Brown", "c" = "blue", "Red");
$array 2 = Array ("A" = "green", "yellow", "red");
$result = Array_diff_uassoc ($array 1, $array 2, "Func");//array ([b] =>brown,[c] =>blue,[0] =>red)

4, Array_intersect--Calculate the intersection of the array, the key name is not used for comparison
$array 1 = Array ("A" = "green", "Red", "blue");
$array 2 = Array ("b" = "green", "yellow", "red");
$result = Array_intersect ($array 1, $array 2);//array ([a] =>green,[0] =>red)

5, ARRAY_INTERSECT_ASSOC--with index check the intersection of the computed array, the key name is also used to compare
$array 1 = Array ("A" = "green", "b" = "Brown", "c" = "blue", "Red");
$array 2 = Array ("A" = "green", "yellow", "red");
$result _array = Array_intersect_assoc ($array 1, $array 2);//array ([A] = green)


Four, pointer operation
1, current--Returns the cell in the array
2, reset--points the inner pointer of the array to the first cell
3, end--points the inner pointer of the array to the last cell
4, next-Moves the inner pointer in the array forward one
5, Prev--Reverse the internal pointer of the array back to a
$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--Returns the current key/value pair in the array and moves the array pointer one step forward
Returns an array of four cells with the key name 0,1,key and value. Unit 0 and key contain the key name of the array cell, 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}


v. Merger
1, array_merge_recursive-merge one or more arrays, if the array has the same key name, the latter value will not overwrite the original value, but attached to the back.
$ar 1 = Array ("color" = = Array ("Favorite" = "Red"), 5);
$ar 2 = Array ("color" = = Array ("Favorite" = "green", "Blue"));
$result = array_merge_recursive ($ar 1, $ar 2);
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 will overwrite the previous one, but the number key name is the same, the following value will not overwrite the original value, but attached to the back.
$array 1 = Array ("Color" = "Red", 2, 4);
$array 2 = Array ("A", "B", "color" = "green", "shape" = "trapezoid", 4);
$result = Array_merge ($array 1, $array 2);
Array ([color] =>green,[0] =>2,[1] =>4,[2] =>a,[3] =>b,[shape] =>trapezoid,[4] = 4)

3, Array_combine-Create an array, use the value of an array as its key name, and the value of another 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)

six, the value
1, Array_slice (Array,offset[,length[,keys])--remove a paragraph from the array
Offset nonnegative, starting at this offset in the array, negative, starting at this distance from the end of the array
Length, the array will have length elements, the negative will terminate at the end of the distance array so far away. Ellipsis starts at offset from the end of the array.
Keys set to TRUE to reserve the key name, otherwise reset the key name


2, Array_rand (Input,[num_req])--random out of the array of one or more cells, num_req indicates how many to take, the default 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])--Remove part of the array and replace with other values, numeric 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, 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: is positive, the offset specified by the value in the array begins to be removed, minus the offset specified by the value at the end of the countdown
Length: Omitted removes all parts of the array from offset to end. Remove so many cells for the regular. The negative removes all the cells from offset to the end of the array ending with length.
If the replacement array is given, the removed cell is replaced by the cells in this array

2, Array_unique--the duplicate values in the Migration Group,
$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-the last element of the array is ejected (out of the stack), the length of the array minus one, the use of this function will reset the array pointer.
$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 cell beginning of the array to the group, the number key will be changed from zero to start counting, the text key will not change
$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)


Eight, insert and fill
1, Array_pad (input,pad_size,pad_value)--fill the array with values to a specified length
If the pad_size is positive, the array is filled to the right and, if negative, 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 an array (-1,-1,-1,-1, 12, 10, 9)
$result = Array_pad ($input, 2, "NoOp");//Not padded

2, Array_push--pressing one or more cells into the end of the array (into the stack)
$stack = Array ("Orange", "banana");
Array_push ($stack, "Apple");//array ([0] = orange,[1] = banana,[2] + apple)

3, Array_unshift--inserts one or more cells at the beginning of the array, the numeric key name is re-counted, 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)--fills the array with the given values
Num: Populated entry Start_index: Specifies the start of the key name value: The values that are populated
$a = Array_fill (5, 4, ' banana ');//array ([5]=>banana,[6]=>banana,[7]=>banana,[8]=>banana)

Nine, callback function
1, Array_filter--Filter the cells in an array with a callback function
function Odd ($var) {return ($var% 2 = = 1);}
function even ($var) {return ($var% 2 = = 0);}
$array 1 = Array ("A" =>1, "B" =>2, "C" =>3, "D" =>4, "E" =>5);
$array 2 = Array (6, 7, 8, 9, 10, 11, 12);

Print_r (Array_filter ($array 1, "odd")); Array ([a] =>1,[c] =>3,[e] = 5)
Print_r (Array_filter ($array 2, "even")); Array ([0] =>6,[2] =>8,[4] =>10,[6] =>12)

2, Array_map--the function of the callback function on the cell 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)

3, Array_reduce--Using callback function iteratively to 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. Other
1, Array_chunk--splitting an array into multiple
$input _array = Array (' A ', ' B ', ' C ', ' d ', ' e ');
Print_r (Array_chunk ($input _array, 2)); The default value is FALSE, and each result array will be indexed with a new zero-based number
Print_r (Array_chunk ($input _array, 2, true));//true represents the original key name in the array

2. Shuffle-an array that disrupts (the order of randomly arranged cells). Delete the original key name and give the new key name

3, In_array--check if there is a value in the array, case sensitive

4. Array and JSON interchange
Json_encode () is to convert the PHP array into JSON.
Json_decode () is the conversion of JSON into a PHP array


An array is defined in PHP as follows: $arr =array (); What is the content of this array?

This array is empty, you can use Print_r (); Look at the results:
Print_r ($arr);

What is $array[] in PHP? $array is an array

What do you mean, you don't know what you're talking about? $array is the number group how to ask?
If you assign a value
$array [] = "1";
$array [] = "2";
So you can,
Delete the words so unset ($array [1]);

http://www.bkjia.com/PHPjc/834017.html www.bkjia.com true http://www.bkjia.com/PHPjc/834017.html techarticle PHP Basics Summary (1) Array of arrays, basic knowledge array One, sort 1, asort--forward sort, keep index relationship 2, arsort--reverse sort, keep index relationship 3, sort--from ...

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