PHP array Common moves _php tutorial

Source: Internet
Author: User
Tags array sort random shuffle shuffle
Reading an array
$arr =array ("xx" = "fork", "yy" = "crooked");
echo "reads the first:". $arr [Number of xx];//statistics array
$array =array (AAA,BBB,CCC,DDD);
echo "Number of arrays:". Count ($array). " n "; Computes the number of array cells or the number of attributes in an object. sizeof ()//fast Array creation
$arr _range = range (0,20,2); Defines the range of values minimum 0 maximum 6 step 2 (difference, optional)
$arr _range2 = Range ("A", "N");
echo "quickly creates a numeric array with range ():";
Print_r ($arr _range);
echo "n";
echo "quickly creates an array of letters with range ():";
Print_r ($arr _range2);
echo "n";//test array
if (Is_array ($arr _range)) {echo "$arr _range is an array!n";} Else{echo "is not an array!n";} Splitting a fraction group
$str = "Piglet, kitten, puppy";
List ($a, $b, $c) =explode (",", $str);
echo "Array split into:". $a. " ". $b. " ". $c;
echo "
";//Add elements to the array header
$oldarr =array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr =array_unshift ($oldarr, "Xiao Sun", "Xiao Zhou", "Xiao Wu");
Print_r ($oldarr); This array variable is in the head and is added to the new
echo "Number:". Print_r ($newarr);//Add elements at the end of the array
$oldarr =array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr =array_push ($oldarr, "Xiao Sun", "Xiao Zhou", "Xiao Wu");
Print_r ($oldarr); This array variable is added to the trailing, new
echo "number is:". Print_r ($newarr);//delete value from array header
$oldarr =array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr =array_shift ($oldarr);
Print_r ($newarr); Returns the deleted value
Print_r ($oldarr); Returns the remaining after deleted//delete value from the tail of the array
$oldarr =array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao");
$newarr =array_pop ($oldarr);
Print_r ($newarr); Returns the deleted value
Print_r ($oldarr); Returns the remaining//anchor elements after being deleted: Search array
$search = "Xiao Liu";
$oldarr =array ("Xiao Zhang", "Xiao Li", "Xiao Wang", "Xiao Zhao", "Xiao Liu");
The IF (In_array ($search, $oldarr)) {echo "array has:". $search. " n ";} The Else{echo "Array does not have:". $search. " n ";} Locating elements: Searching for associative array "keys"
$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 "does not exist!"} Anchor element: Search associative array "value"
$oldarr ["Fruit 1"]= "watermelon";
$oldarr ["Fruit 2"]= "peach";
$oldarr ["Fruit 3"]= "Grape";
$found =array_search ("Watermelon", $oldarr);
if ($found) {printf (the value of%s is:%s N, $found, $oldarr [$found]);} else {echo "does not exist!"} Gets the "key" in the array
$oldarr =array ("Strength" = "boxing", "speed" = "Running");
while ($key = key ($oldarr)) {
printf ("%s
", $key);
Next ($oldarr); Each time the key does not move the pointer, you need next to move the pointer
}//gets the "value" in the array
$oldarr =array ("Strength" = "boxing", "speed" = "Running");
while ($val = current ($oldarr)) {
printf ("%s
", $val);
Next ($oldarr); Each time the key does not move the pointer, need next to move the pointer backwards prev () move forward a reset () is the first end of the move () is the moving end
}//"Keys and Values" in the array
$arr =array ("One", "one", "one", "three");
foreach ($arr as $value) {
echo "Value:". $value. "
";
}//statistics array elements appear frequency (result is number of values)
$arr =array ("One", "I", "three", "one", "three");
Print_r (Array_count_values ($arr));//determine unique array element (output non-repeating content)
$arr =array ("One", "I", "three", "one", "three");
Print_r (Array_unique ($arr));//inverse array element order (key and value)
$arr =array ("One", "I", "three", "four", "five");
Print_r (Array_reverse ($arr));//permutation arrays and values
$arr =array ("One", "I", "three", "four", "five");
Print_r (Array_flip ($arr));//Positive ordinal group sort (key and value associations are not persisted)
$arr =array ("One", "I", "three", "four", "five");
Sort ($arr);
Print_r ($arr);//Positive ordinal group sort (key and value Association hold)
$arr =array ("One", "I", "three", "four", "five");
Asort ($arr);
Print_r ($arr);//reverse array ordering (key and value association not persisted)
$arr =array ("One", "I", "three", "four", "five");
Rsort ($arr);
Print_r ($arr);//reverse array ordering (key and Value Association hold)
$arr =array ("One", "I", "three", "four", "five");
Arsort ($arr);
Print_r ($arr);//Natural sort Array (classic sort)
$arr =array ("A1", "A3", "A10", "A22", "A19");
Natsort ($arr);
Print_r ($arr);//Natural sort Array (classic sort, case insensitive)
$arr =array ("A1", "A3", "A10", "A22", "A19");
Natcasesort ($arr);
Print_r ($arr);//Key value array sort
Ksort ();
Sort the array keys in reverse order
Krsort ();
Sort by user-defined rules
$arr =array (' 2009-02-06 ', ' 2009-02-10 ', ' 2009-02-13 ', ' 10-06-2009 ', ' 9-17-2009 ');
Sort ($arr);
Print_r ($arr);//merge, Split, join, decompose 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);//recursive append array
$arr _a = Array ("AK" = "xx", "AV", "AO", "AE"); With the key of the first array
$arr _b = Array ("AKSSJD" = "XASD", "Aosdwe", "aesadlkj", "avxiwqlk"); With the value of the second array
$newarr = Array_combine ($arr _a, $arr _b);
Print_r ($newarr);//Link two arrays (Key auto sequential number, value merge, single display with key, key same value will overwrite)
$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); Shuffle (the array is scrambled), each time the result is different
Print_r ($newarr);//Split fractional group
$oldarr =array ("Watermelon", "apple", "grapefruit", "banana", "Mango", "Kiwi", "Hawthorn");
$newarr =array_slice ($oldarr, 2,-2); Remove the first two, the last two
Print_r ($newarr); The new array
Print_r ($oldarr); All original array//join array (form a new two array)
$oldarr =array ("Watermelon", "apple", "grapefruit", "banana", "Mango", "Kiwi", "Hawthorn");
$newarr =array_splice ($oldarr, 2);
Print_r ($newarr); The new array
Print_r ($oldarr); Deleted array//array of communication
$new _a=array ("A", "B", "C", "D", "E", "F"); The first array of "keys and values" is the standard, and the 3 array "values" have exactly the same 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);//Find the intersection of associative arrays (like this one above)
ARRAY_INTERSECT_ASSOC () This consideration "key" is all the same, if "key" and "value" are the same output results
Find the difference set of arrays (this is just the opposite of intersect ())
$new _a=array ("A", "B", "C", "D", "E", "F"); The first array of "keys and values" is the standard, and 3 array "values" have no output at all
$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);//Find the intersection of associative arrays (like this one above)
ARRAY_DIFF_ASSOC () This consideration "key" is also the same, if "key" and "value" have no output result
Returns a set of "keys" that are immediately
$oldarr = Array ("AAA" = "DASD", "bbb" = "axsae", "CCC" = "FDVDF", "ddd" = "EWMCC");
$newarr = Array_rand ($oldarr, 2);
Print_r ($newarr);//random shuffle array, randomly disturb "value" order
$oldarr = Array (a=> "SD", "SC", "XE", "Po", "Ed");
Shuffle ($oldarr);
Print_r ($oldarr);//sum of values in an array
$oldarr = Array (, "Hello", 75);
$newarr = Array_sum ($oldarr);
print $newarr;//decompose an array into a multidimensional array
$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 a number that does not exist in the array */
$arr =array (2,3,5,7,9,11,13,14,16,23,26,28,29,31,32,33,35,37,39,41,46,49);
for ($i =0; $i
$randnum = rand (0,50);
if (In_array ($randnum, $arr)) {
Break
}else{
printf ("There is no%d
", $randnum);
}

http://www.bkjia.com/PHPjc/371687.html www.bkjia.com true http://www.bkjia.com/PHPjc/371687.html techarticle //Read array $arr =array (xx= fork, yy= Crooked); Echo reads the first:. $arr [xx];//Statistics array number $array =array (AAA,BBB,CCC,DDD); The number of echo arrays is:. COUNT ($ Array). n;//Calculation List ...

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