Php array function example tutorial

Source: Internet
Author: User
Php array function example tutorial

  1. $ Arr = array ("name" => "user1", "age" => "30", "sex" => "man ");

  2. Foreach ($ arr as $ key => $ val ){
  3. $ Keys [] = $ key;
  4. $ Vals [] = $ val;
  5. }
  6. Echo"
    ";  
  7. print_r($keys);
  8. echo "
  9. ";
  10. Echo "";
  11. Echo"
    ";  
  12. print_r($vals);
  13. echo "
  14. ";

  15. ?>

2. use of array_values

  1. $ Arr = array ("name" => "user1", "age" => "30", "sex" => "man ");
  2. $ Keys = array_values ($ arr );
  3. Echo"
    ";  
  4. print_r($keys);
  5. echo "
  6. ";
  7. ?>

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 ();

  1. $ Arr = array ("user1" => );
  2. Function older ($ var ){
  3. Return ($ var> 60 );
  4. }
  5. $ Arr2 = array_filter ($ arr, "older ");
  6. Echo"
    ";  
  7. print_r($arr2);
  8. echo "
  9. ";
  10. ?>

2. array_map (); reference parameter: Requirement: Array Value auto-Increment 1

  1. Function show (& $ arr ){
  2. Foreach ($ arr as $ key => $ val ){
  3. $ Arr [$ key] = $ val + 1;
  4. }
  5. }

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 ();

  1. $ Arr = array ("user1" => 10, "B" => 1, "c" => 3, "d" => 30 );
  2. $ Arr2 = array_flip ($ arr );
  3. Ksort ($ arr2 );
  4. Echo"
    ";  
  5. print_r($arr2);
  6. echo "
  7. ";
  8. ?>

Natsort ();

  1. $ Array1 = $ array2 = array ("img12.png", "img10.png", "img2.png", "img1.png ");
  2. Sort ($ array1 );
  3. Echo "Standard sorting \ n ";
  4. Print_r ($ array1 );
  5. Natsort ($ array2 );
  6. Echo "\ nNatural order sorting \ n ";
  7. Print_r ($ array2 );
  8. ?>

Multi-array sorting:

  1. $ Arr = array ("aaa", "bbbbbbbbbbb", "cc", "ddddd ");
  2. // Requirement:
  3. // 1. Sort by title length
  4. // 2. the title length is changed to the key of the title string
  5. // Extract the length of the value in the array and use it as a new array
  6. // Strlen ($ val) retrieves the string length
  7. Foreach ($ arr as $ val ){
  8. $ Lens [] = strlen ($ val );
  9. }
  10. Array_multisort ($ lens, SORT_ASC, $ arr); // sorts the array. sort the second array by the first array. SORT_ASC indicates ascending sorting.
  11. Sort ($ lens );
  12. $ Arr2 = array_combine ($ lens, $ arr); // The first array acts as the key corresponding to the second array and returns a new array
  13. Echo"
    ";  
  14. print_r($arr2);
  15. echo "
  16. ";
  17. ?>

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

  1. $ Str = "php, js, html, ces, p ";
  2. $ Arr = explode (",", $ str );
  3. Echo"
    ";  
  4. print_r($arr);
  5. echo "
  6. ";
  7. ?>

2. inplode (); combines the array into a string

  1. $ Str = "php, js, html, ces, p ";

  2. $ Arr = explode (",", $ str );
  3. $ Str2 = implode ("-", $ arr );
  4. Echo"
    ";  
  5. print_r($str2);
  6. echo "
  7. ";
  8. ?>

  9. $ Str = "php, js, html, ces, p ";

  10. $ Arr = explode (",", $ str );
  11. $ Arr2 = array_reverse ($ arr); // sort the values in the array in reverse order.
  12. $ Str2 = implode ("-", $ arr2 );
  13. Echo"
    ";  
  14. print_r($str2);
  15. echo "
  16. ";
  17. ?>

Array_slice ();

  1. // The screenshot is always taken from the back.
  2. $ Arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg ");
  3. $ Arr2 = array_slice ($ arr,); // Two aa bb instances are intercepted from 0.
  4. $ 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
  5. Echo"
    ";  
  6. print_r($arr3);
  7. echo "
  8. ";
  9. ?>

Not only can be removed, but can also be added

  1. $ Arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg ");
  2. $ 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.
  3. Echo"
    ";  
  4. print_r($arr2);
  5. echo "
  6. ";
  7. Echo"
    ";  
  8. print_r($arr);
  9. echo "
  10. ";
  11. ?>

Array_merge ();

  1. $ A = array ("aa", "bb", "cc ");
  2. $ B = array ("dd", "ee", "ff", "gg ");
  3. $ Arr = array_merge ($ a, $ B );
  4. Echo"
    ";  
  5. print_r($arr);
  6. echo "
  7. ";
  8. ?>

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.

  1. $ Arr = array ("aa", "bb", "cc", "dd", "ee", "ff", "gg ");

  2. // Randomly disrupt the original array order
  3. Shuffle ($ arr );
  4. // Retrieve the first three of the arrays
  5. $ Arr2 = array_slice ($ arr, 0, 3 );
  6. Echo"
    ";  
  7. print_r($arr2);
  8. echo "
  9. ";
  10. ?>
  11. // Outputs a four-character verification code at random:

  12. // Retrieve the array of 1-9 a-z A-Z

  13. $ A = range (1, 9 );
  14. $ B = range (a, z );
  15. $ C = range (A, Z );
  16. // Combine the three numbers and
  17. $ D = array_merge ($ a, $ B, $ c );
  18. // Disrupt the merged array
  19. Shuffle ($ d );
  20. // Obtain the first 4 digits after merging
  21. $ E = array_slice ($ d, 0, 4 );
  22. // Convert the $ e array into a string
  23. $ F = join ("", $ e );
  24. Echo $ f;
  25. ?>

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.