Learn more about using PHP array Functions

Source: Internet
Author: User

PHP array functions are quite common, So I studied PHP array functions and shared them here, hoping to be useful to you. Today, we are going to learn about PHP array functions. arrays are a type of data that is frequently used. Learning to process arrays will make your website easy.

For example, your website database contains a large article. Now you want to display 20 titles of entertainment news on a page, do you want to use 20 variables after these 20 titles are extracted from the database? I think you will not be so stupid, so what variables can be used to fully receive the 20 pieces of data? This requires array variables. What is an array? An array is actually a data set, which is equivalent to a data container. A lot of data is stored in it. We can store or retrieve it in a certain way, you can also sort the data in it, and check whether there is any data we want.

1. array_multisort () can be used to sort multiple arrays or multi-dimensional arrays at a time or by one dimension. The original key-name association is retained during sorting, and the implementation function is similar to ORDERBY in SQL.

 
 
  1. $Ar1=Array("10", 100,100, "");
  2. $Ar2=Array(1, 3, "2", 1 );
  3. Array_multisort ($ ar1, $ ar2 );
  4. ?> 
  5. // Return results (maintaining the relevance of the original array)
  6. $Ar1=Array([0] =>10 [1] =>A [2] =>100 [3] =>100)
  7. $Ar2=Array([0] =>1 [1] =>1 [2] =>2 [3] =>3)
  8.  
  9. // Rank students' scores
  10. $Grade=Array("Score" =>Array (70,95, 70.0, 60, "70 "),
  11. "Name" =>Array ("ZhangSan", "LiSi", "WangWu ",
  12. "ZhaoLiu", "LiuQi "));
  13. Array_multisort ($ grade ["score"], SORT_NUMERIC, SORT_DESC,
  14. // Take the score as a value, sorted from high to low
  15. $ Grade ["name"], SORT_STRING, SORT_ASC );
  16. // Use the name as a string in ascending order
  17. ?> 
  18. // Output result
  19.  
  20. Array (
  21. 'Score '=> 
  22. Array (
  23. 0=>95,
  24. 1=>'70 ',
  25. 2=>70,
  26. 3=>70,
  27. 4=>60,
  28. ),
  29. 'Name' => 
  30. Array (
  31. 0=>'Lisi ',
  32. 1=>'Liuqi ',
  33. 2=>'Wangwu ',
  34. 3=>'Hangsan ',
  35. 4=>'Zhaoliu ',
  36. ),
  37. )

2. array_rand () and shuffle (), one is to randomly retrieve one or more values from the array, the other is to disrupt the order of values in the array, and then you can retrieve values from it, they all implement the random data extraction function from the array and can be used to implement functions such as lottery.

3. array_reduce () iterates the callback function into every unit in the input array, so that the array is simplified to a single value. If the optional parameter initial is specified, the parameter is treated as the first value in the array, or if the array is empty, it is returned as the final value.

 
 
  1. <?Php 
  2. Functionrsum ($ v, $ w ){
  3. $ V + = $ w;
  4. Return $ v;
  5. }
  6.  
  7. Functionrmul ($ v, $ w ){
  8. $ V * = $ w;
  9. Return $ v;
  10. }
  11.  
  12. $A=Array(1, 2, 3, 4, 5 );
  13. $X=Array();
  14. // Implement the array_sum () function
  15. $B=Array_reduce($ A, "rsum ");//115= 1 + 2 + 3 + 4 + 5
  16.  
  17. $C=Array_reduce($ A, "rmul", 10 );//11200= 1*2*3*4*5*10
  18. $D=Array_reduce($ X, "rsum", 1); // 1
  19. ?> 

This function can calculate the sum of an array (product, and other 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.