Learn how to use PHP array functions _php Tutorial

Source: Internet
Author: User
Tags learn php
PHP Array function is still more commonly used, so I studied the PHP array function, here to take out and share with you, hope to be useful to everyone. Today we learn PHP array function, array is a type of data, it is very high frequency, learn to deal with the array will make your site handy.

For example: Your site database has a Dali article, now you want to display 20 entertainment news on a page title, the 20 headings from the database after you want to use 20 variables to represent it? I think you are not so stupid, so what variables can fully receive the 20 data? This will use the array variable. What is an array? Array is actually a data collection, equivalent to a data container, a lot of data stored in the inside, we can be stored in or out of a certain way, but also the data inside it can be sorted and other operations, but also to check if there is no data we want, and so on.

1.array_multisort () Sorting multiple arrays or multidimensional arrays can be used to sort multiple arrays at once or to sort multidimensional arrays based on a dimension. Sorting preserves the original key-name associations, and implements the same functionality as in SQL.

 
 
  1. $ ar1 = Array ("Ten", 100,100, "a");
  2. $ ar2 = Array (1, 3, "2", 1);
  3. Array_multisort ($ar 1, $ar 2);
  4. ?>
  5. Returns the result (preserving the association of the original array)
  6. $ ar1 = Array ([0]=>10[1]=>a[2]=>100[3]= > + )
  7. $ ar2 = Array ([0]=>1[1]=>1[2]=>2[3]= > 3)
  8. Achieving student Performance rankings
  9. $ Grade = Array ("Score" =>Array (70,95,70.0,60, "" "),
  10. "Name" = > Array ("Zhangsan", "LiSi", "Wangwu" ,
  11. "Zhaoliu", "Liuqi"));
  12. Array_multisort ($grade ["Score"],sort_numeric,sort_desc,
  13. Sort fractions as numbers, from high to low
  14. $grade ["name"],SORT_STRING,SORT_ASC);
  15. Sort names as strings, from small to large
  16. ?>
  17. Output results
  18. Array
  19. ' score ' = >
  20. Array
  21. 0 = > ,
  22. 1 = > ' A ',
  23. 2 = > ,
  24. 3 = > ,
  25. 4 = > ,
  26. ),
  27. ' name ' = >
  28. Array
  29. 0 = > ' LiSi ',
  30. 1 = > ' Liuqi ',
  31. 2 = > ' Wangwu ',
  32. 3 = > ' Zhangsan ',
  33. 4 = > ' Zhaoliu ',
  34. ),
  35. )

2.array_rand () and Shuffle (), one is to take one or more values randomly from the array, the other is to scramble the order of the values in the array, and then remove the values from them, all of which implement the function of randomly extracting data from the array, which can be used to implement functions such as sweepstakes.

3.array_reduce () iterates the callback function functions into each cell in the input array, simplifying the array to a single value. If an optional parameter, initial, is specified, the parameter is treated as the first value in the array or, if the array is empty, as the final return value.

 
 
  1. php
  2. Functionrsum ($v, $w) {
  3. $v + = $w;
  4. Return$v;
  5. }
  6. Functionrmul ($v, $w) {
  7. $v *= $w;
  8. Return$v;
  9. }
  10. $ a = Array (1,2,3,4,5);
  11. $ x = Array ();
  12. Implementing the Array_sum () function
  13. $ b = Array_reduce ($a, "rsum");// 1 the =1+2+3+4+5
  14. $ C = Array_reduce ($a, "rmul");// 1 - =1*2*3*4*5*10
  15. $ D = Array_reduce ($x, "Rsum", 1);//1
  16. ?>

This function can be implemented to find an array of and (product, poor function)


http://www.bkjia.com/PHPjc/446540.html www.bkjia.com true http://www.bkjia.com/PHPjc/446540.html techarticle PHP Array function is still more commonly used, so I studied the PHP array function, here to take out and share with you, hope to be useful to everyone. Today we are learning PHP array functions, ...

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