$ Arr = array ("name" => "user1", "age" => "30", "sex" => "man ");
- Foreach ($ arr as $ key => $ val ){
- $ Keys [] = $ key;
- $ Vals [] = $ val;
- }
- Echo"
"; - print_r($keys);
- echo "
";
- Echo "";
- Echo"
"; - print_r($vals);
- echo "
";
?>
2. use of array_values
- $ Arr = array ("name" => "user1", "age" => "30", "sex" => "man ");
- $ Keys = array_values ($ arr );
- Echo"
"; - print_r($keys);
- echo "
";
- ?>
-
Array_values (); // obtain the value array_keys () in the array; // Obtain the Jian in_array () in the array; // check whether a value is in the array array_key_exists (); // check whether a key is in the array array_flip (); // the key and value pairs are called array_reverse (); the values in the array are reversed. Counts the elements and uniqueness of the array. count (); 2. array_count_values (); // counts the number of occurrences of each value in the array. 3. array_unique (); // delete the function that uses the callback function to process the array repeatedly: 1. array_filter ();
- $ Arr = array ("user1" => );
- Function older ($ var ){
- Return ($ var> 60 );
- }
- $ Arr2 = array_filter ($ arr, "older ");
- Echo"
"; - print_r($arr2);
- echo "
";
- ?>
2. array_map (); reference parameter: Requirement: Array Value auto-Increment 1
- Function show (& $ arr ){
- Foreach ($ arr as $ key => $ val ){
- $ Arr [$ key] = $ val + 1;
- }
- }
-
Array sorting function 1. sort (); ascending, key2.rsort (); descending, key3.asort (); ascending, key4.arsort (); descending, key5.ksort (); sort keys in ascending order. krsort (); sort by key in descending order 7. natsort (); Natural numbers sort ascending order, ratio piece img2.jpg 8. natcasesort (); ignore case-insensitive ascending order 9. multisort (); Multi-array sorting ksort ();
- $ Arr = array ("user1" => 10, "B" => 1, "c" => 3, "d" => 30 );
- $ Arr2 = array_flip ($ arr );
- Ksort ($ arr2 );
- Echo"
"; - print_r($arr2);
- echo "
";
- ?>
Natsort ();
- $ Array1 = $ array2 = array ("img12.png", "img10.png", "img2.png", "img1.png ");
- Sort ($ array1 );
- Echo "Standard sorting \ n ";
- Print_r ($ array1 );
- Natsort ($ array2 );
- Echo "\ nNatural order sorting \ n ";
- Print_r ($ array2 );
- ?>
Multi-array sorting:
- $ Arr = array ("aaa", "bbbbbbbbbbb", "cc", "ddddd ");
- // Requirement:
- // 1. Sort by title length
- // 2. the title length is changed to the key of the title string
- // Extract the length of the value in the array and use it as a new array
- // Strlen ($ val) retrieves the string length
- Foreach ($ arr as $ val ){
- $ Lens [] = strlen ($ val );
- }
- Array_multisort ($ lens, SORT_ASC, $ arr); // sorts the array. sort the second array by the first array. SORT_ASC indicates ascending sorting.
- Sort ($ lens );
- $ Arr2 = array_combine ($ lens, $ arr); // The first array acts as the key corresponding to the second array and returns a new array
- Echo"
"; - print_r($arr2);
- echo "
";
- ?>
-
Splitting, merging, splitting, and combining functions 1. explode (); 2. inplode (); // join (); 3. array_slice (); array truncation 4. array_splice (); array cropping 5. array-merge (); merge multiple arrays 6. array_combine (); merges two arrays. the first array is used as the key, and the last array is used as the value7.array _ intersect (). 8. array_diff (); find the differences between the two arrays, according to the first parameter 9. array_pop (); the last value is displayed, and 10 is returned. array_push (); press a value from the last position and return 11 elements. array_shift (); delete a value 12 from the position before the wash. array_unshift (); press a value from the beginning
- $ Str = "php, js, html, ces, p ";
- $ Arr = explode (",", $ str );
- Echo"
"; - print_r($arr);
- echo "
";
- ?>
2. inplode (); combines the array into a string
$ Str = "php, js, html, ces, p ";
- $ Arr = explode (",", $ str );
- $ Str2 = implode ("-", $ arr );
- Echo"
"; - print_r($str2);
- echo "
";
- ?>
$ Str = "php, js, html, ces, p ";
- $ Arr = explode (",", $ str );
- $ Arr2 = array_reverse ($ arr); // sort the values in the array in reverse order.
- $ Str2 = implode ("-", $ arr2 );
- Echo"
"; - print_r($str2);
- echo "
";
- ?>
-
Array_slice ();
- // The screenshot is always taken from the back.
- $ Arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg ");
- $ Arr2 = array_slice ($ arr,); // Two aa bb instances are intercepted from 0.
- $ Arr3 = array_slice ($ arr,-3, 2); // This parameter indicates that two requests are intercepted from the last three places to the last three places. // ee ff
- Echo"
"; - print_r($arr3);
- echo "
";
- ?>
Not only can be removed, but can also be added
- $ Arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg ");
- $ Arr2 = array_splice ($ arr, 0, 3, array ("hh", "ii", "jj", "kk ")); // directly retrieve the value of the original array and change the original array. the original array is the value left after the original array is removed.
- Echo"
"; - print_r($arr2);
- echo "
";
- Echo"
"; - print_r($arr);
- echo "
";
- ?>
Array_merge ();
- $ A = array ("aa", "bb", "cc ");
- $ B = array ("dd", "ee", "ff", "gg ");
- $ Arr = array_merge ($ a, $ B );
- Echo"
"; - print_r($arr);
- echo "
";
- ?>
-
Other useful array processing functions: 1. array_rand (); // A random key2.range (); // retrieves an array of a certain range. 3. shuffle (); // disrupt the function of array 4. array_sum (); // calculate the sum of all people in the array (total score). If the sum of keys in the array is calculated, array_flip () can be used to check the health and value of the array, then we can calculate the sum of keys.
$ Arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg ");
- // Randomly disrupt the original array order
- Shuffle ($ arr );
- // Retrieve the first three of the arrays
- $ Arr2 = array_slice ($ arr, 0, 3 );
- Echo"
"; - print_r($arr2);
- echo "
";
- ?>
- // Outputs a four-character verification code at random:
// Retrieve the array of 1-9 a-z A-Z
- $ A = range (1, 9 );
- $ B = range (a, z );
- $ C = range (A, Z );
- // Combine the three numbers and
- $ D = array_merge ($ a, $ B, $ c );
- // Disrupt the merged array
- Shuffle ($ d );
- // Obtain the first 4 digits after merging
- $ E = array_slice ($ d, 0, 4 );
- // Convert the $ e array into a string
- $ F = join ("", $ e );
- Echo $ f;
- ?>
|