Common PHP array methods. Read the array $ arrarray (xx cross, yy); echo reads the first one :. $ arr [xx]; number of arrays $ arrayarray (aaa, bbb, ccc, ddd); number of echo arrays :. count ($ array ). n; calculate the array single // read the array
$ Arr = array ("xx" => "", "yy" => "");
Echo "read the first one:". $ arr [xx]; // count the number of arrays
$ Array = array (aaa, bbb, ccc, ddd );
Echo "the number of arrays is:". count ($ array). "n"; // calculate the number of array units or the number of attributes in the object is equivalent to sizeof () // quickly create an array
$ Arr_range = range (, 2); // the minimum range of the defined value is 0. The maximum 6 step size is 2 (the difference is optional)
$ Arr_range2 = range ("A", "N ");
Echo "use range () to quickly create a number array :";
Print_r ($ arr_range );
Echo "n ";
Echo "use range () to quickly create an array of letters :";
Print_r ($ arr_range2 );
Echo "n"; // test the array
If (is_array ($ arr_range) {echo "$ arr_range is an array! N ";} else {echo" is not an array! N ";} // split the array
$ Str = "piglet, kitten, puppy ";
List ($ a, $ B, $ c) = explode (",", $ str );
Echo "array split:". $ a. "". $ B. "". $ c;
Echo"
"; // Add an element to the array header
$ Oldarr = array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao ");
$ Newarr = array_unshift ($ oldarr, "Sun", "Zhou", "Wu ");
Print_r ($ oldarr); // This array variable is added to the header
Echo ":". print_r ($ newarr); // add an element at the end of the array
$ Oldarr = array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao ");
$ Newarr = array_push ($ oldarr, "Sun", "Zhou", "Wu ");
Print_r ($ oldarr); // This array variable is at the end and added to the new
Echo ":". print_r ($ newarr); // delete the value from the array header
$ Oldarr = array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao ");
$ Newarr = array_shift ($ oldarr );
Print_r ($ newarr); // return the deleted value.
Print_r ($ oldarr); // return the remaining value after deletion // delete the value from the end of the array
$ Oldarr = array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao ");
$ Newarr = array_pop ($ oldarr );
Print_r ($ newarr); // return the deleted value.
Print_r ($ oldarr); // return the remaining elements after deletion // The positioning element searches for an array.
$ Search = "Xiao Liu ";
$ Oldarr = array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao", "Xiao Liu ");
If (in_array ($ search, $ oldarr) {echo "in the array :". $ search. "n";} the else {echo "array does not contain :". $ search. "n";} // positioning element: Search associated array "key"
$ Oldarr ["fruit 1"] = "watermelon ";
$ Oldarr ["fruit 2"] = "peach ";
$ Oldarr ["fruit 3"] = "grape ";
If (array_key_exists ("fruit 2", $ oldarr) {printf ("the fruit you selected is: % s n", $ oldarr ["fruit 2"]);} else {echo "this does not exist! ";}// Positioning element: Search associated array" value"
$ Oldarr ["fruit 1"] = "watermelon ";
$ Oldarr ["fruit 2"] = "peach ";
$ Oldarr ["fruit 3"] = "grape ";
$ Found = array_search ("watermelon", $ oldarr );
If ($ found) {printf ("% s value: % s n", $ found, $ oldarr [$ found]);} else {echo "this does not exist! ";} // Obtain the" key "in the array"
$ Oldarr = array ("power" => "boxing", "speed" => "running ");
While ($ key = key ($ oldarr )){
Printf ("% s
", $ Key );
Next ($ oldarr); // every time the key does not move the pointer, you need to move the pointer next
} // Obtain the "value" in the array"
$ Oldarr = array ("power" => "boxing", "speed" => "running ");
While ($ val = current ($ oldarr )){
Printf ("% s
", $ Val );
Next ($ oldarr); // every time the key does not move the pointer, next is required to move the pointer prev () forward a reset () is to move the first end () is to move the end
} // Review the "key and value" in the array"
$ Arr = array ("one", "two", "three ");
Foreach ($ arr as $ value ){
Echo "Value:". $ value ."
";
} // Counts the occurrence frequency of array elements (the result is the number of values)
$ Arr = array ("one", "two", "three", "two", "three ");
Print_r (array_count_values ($ arr); // identifies a unique array element (outputs non-repeated content)
$ Arr = array ("one", "two", "three", "two", "three ");
Print_r (array_unique ($ arr); // reverse the order of array elements (key and value)
$ Arr = array ("one", "two", "three", "four", "five ");
Print_r (array_reverse ($ arr); // replace the array and value
$ Arr = array ("one", "two", "three", "four", "five ");
Print_r (array_flip ($ arr); // sort the forward-order array (key-value Association is not maintained)
$ Arr = array ("one", "two", "three", "four", "five ");
Sort ($ arr );
Print_r ($ arr); // sort the array in positive order (key-value Association persistence)
$ Arr = array ("one", "two", "three", "four", "five ");
Asort ($ arr );
Print_r ($ arr); // sort the array in reverse order (key-value Association is not maintained)
$ Arr = array ("one", "two", "three", "four", "five ");
Rsort ($ arr );
Print_r ($ arr); // sort the array in reverse order (key-value Association persistence)
$ Arr = array ("one", "two", "three", "four", "five ");
Arsort ($ arr );
Print_r ($ arr); // natural sorting array (classic sorting)
$ Arr = array ("a1", "a3", "a10", "a22", "a19 ");
Natsort ($ arr );
Print_r ($ arr); // natural sorting array (classic sorting, case insensitive)
$ Arr = array ("A1", "a3", "a10", "A22", "a19 ");
Natcasesort ($ arr );
Print_r ($ arr); // sort the array by the key value
// Ksort ();
// Sort the array keys in reverse order
// Krsort ();
// Sort by custom rules
$ Arr = array ('2017-02-06 ', '2017-02-10', '2017-02-13 ', '10-06-2009', '9-17-2009 ');
Sort ($ arr );
Print_r ($ arr); // merge, split, join, and split an array (recursively merge one or more arrays, note the quotation marks of the key)
$ Arr_a = array ("John" => 100, "James" => 85 );
$ Arr_ B = array ("Micky" => 78, "John" => 45 );
$ Newarr = array_merge_recursive ($ arr_a, $ arr_ B );
Print_r ($ newarr); // recursively append an array
$ Arr_a = array ("AK" => "xx", "AV", "AO", "AE"); // use the key of the first array
$ Arr_ B = array ("Akssjd" => "xasd", "Aosdwe", "Aesadlkj", "Avxiwqlk"); // use the value of the second array
$ Newarr = array_combine ($ arr_a, $ arr_ B );
Print_r ($ newarr); // link two arrays (keys are automatically ordered numbers, values are merged, keys are displayed separately, and values with the same keys are overwritten)
$ Arr_a = array ("W", a => "O", "F", "R", "C ");
$ Arr_ B = array ("2", "6", "4", "7", "3", a => "x", "0 ", "9 ");
$ Newarr = array_merge ($ arr_a, $ arr_ B );
Shuffle ($ newarr); // shuffling (disrupt the array), with different results each time
Print_r ($ newarr); // split the array
$ Oldarr = array ("watermelon", "Apple", "grapefruit", "banana", "Mango", "Kiwi", "Hawthorn ");
$ Newarr = array_slice ($ oldarr, 2,-2); // remove the first two and the last two
Print_r ($ newarr); // New array
Print_r ($ oldarr); // All original arrays // join arrays (form two new arrays)
$ Oldarr = array ("watermelon", "Apple", "grapefruit", "banana", "Mango", "Kiwi", "Hawthorn ");
$ Newarr = array_splice ($ oldarr, 2 );
Print_r ($ newarr); // New array
Print_r ($ oldarr); // The deleted array // Evaluate the communication of the array
$ New_a = array ("A", "B", "C", "D", "E", "F "); // based on the array of the first line of "key and value", the three arrays "value" have identical output
$ New_ B = array ("B", "X", "E", "R", "W", "P ");
$ New_c = array ("Z", "N", "B", "W", "E", "C ");
$ Newarr = array_intersect ($ new_a, $ new_ B, $ new_c );
Print_r ($ newarr); // calculates the intersection of correlated arrays (similar to the preceding one)
// Array_intersect_assoc (). The "key" is the same. if the "key" and "value" are the same, the output result is displayed.
// Calculate the difference set of the array (this is the opposite of intersect)
$ New_a = array ("A", "B", "C", "D", "E", "F "); // based on the array of the first line of "key and value", the three arrays "value" have no output
$ New_ B = array ("B", "X", "E", "R", "W", "P ");
$ New_c = array ("Z", "N", "B", "W", "E", "C ");
$ Newarr = array_diff ($ new_a, $ new_ B, $ new_c );
Print_r ($ newarr); // calculates the intersection of correlated arrays (similar to the preceding one)
// Array_diff_assoc (). The "key" is the same. if neither "key" nor "value" is output
// Return a set of "keys"
$ Oldarr = array ("aaa" => "dasd", "bbb" => "axsae", "ccc" => "fdvdf ", "ddd" => "ewmcc ");
$ Newarr = array_rand ($ oldarr, 2 );
Print_r ($ newarr); // randomly shuffles the array to randomly disrupt the "value" order
$ Oldarr = array (a => "sd", "SC", "xe", "po", "ed ");
Shuffle ($ oldarr );
Print_r ($ oldarr); // sum the values in the array
$ Oldarr = array (36, "hello", 75 );
$ Newarr = array_sum ($ oldarr );
Print $ newarr; // splits an array into multi-dimensional arrays.
$ Oldarr = array ("A", "B", "C", "D", "E", "F", "G", "H ", "I", "J", "K", "L", "M", "N", "O", "P", "Q ", "R"); // shuffle ($ oldarr); // wash the cards
$ Newarr = array_chunk ($ oldarr, 4 );
Print_r ($ newarr );
/* Find the number that does not exist in the array */
$ Arr = array );
For ($ I = 0; $ I $ Randnum = rand (0, 50 );
If (in_array ($ randnum, $ arr )){
Break;
} Else {
Printf ("% d does not exist
", $ Randnum );
}
Outputs $ arr = array (xx = cross, yy = ); echo reads the first one :. $ arr [xx]; // counts the number of arrays $ array = array (aaa, bbb, ccc, ddd); the number of echo arrays is :. count ($ array ). n; // calculate a single array...