PHP 2D array sorting using key implementation _ PHP Tutorial

Source: Internet
Author: User
PHP uses keys to sort two-dimensional arrays. PHP arrays have one-dimensional arrays instead of arrays. here we will analyze the PHP 2D array sorting techniques. PHP, an abbreviated name for nesting, is an English super Text preprocessing language (PHP: Hy PHP array has a one-dimensional array and not an array. here we will analyze the PHP 2D array sorting technique. PHP, an abbreviated name for nesting, is the abbreviation of the English Super Text preprocessing language (PHP: Hypertext Preprocessor. PHP is an embedded HTML language. it is a scripting language that executes HTML documents embedded on the server. the language style is similar to the C language, it is widely used by many website programmers. The unique PHP syntax is a mix of C, Java, Perl, and PHP self-innovative syntaxes.

It can execute dynamic web pages more quickly than CGI or Perl. Compared with other programming languages, PHP embeds programs into HTML documents for execution. the execution efficiency is much higher than the CGI that generates HTML tags completely; compared with the scripting language JavaScript, which is also embedded in HTML documents, PHP is executed on the server, making full use of the server performance; the PHP execution engine also stores PHP programs frequently accessed by users in the memory. when other users access this program again, they do not need to re-compile the program, you only need to directly execute the code in the memory, which is also one of the efficient implementations of PHP. PHP has very powerful functions, all CGI or JavaScript functions can be implemented in PHP, and supports almost all popular databases and operating systems.

PHP uses the asort function to sort two-dimensional arrays. However, if multi-dimensional arrays are sorted by an index value. In general, such sorting will introduce temporary arrays to store intermediate variables. for example, key values can be sorted by Keys to sort multi-dimensional arrays. The following section is sorted by the time value in the two-dimensional array:

PHP 2D array sorting code:

 
 
  1. Php
  2. // Sort two-dimensional arrays in descending order with the time InputDate as the key value
  3. Function array_sort ($ arr, $ keys, $ type ){
  4. $Keysvalue=Array();
  5. $I=0;
  6. Foreach ($ arr as $Key=>$ Val ){
  7. $ Val [$ keys] = str_replace ("-", "", $ val [$ keys]);
  8. $ Val [$ keys] = str_replace ("", "", $ val [$ keys]);
  9. $ Val [$ keys] = str_replace (":", "", $ val [$ keys]);
  10. $ Keysvalue [] = $ val [$ keys];
  11. }
  12. Asort ($ keysvalue); // Sort key values
  13. Reset ($ keysvalue); // The pointer points to the first array.
  14. Foreach ($ keysvalue as $Key=>$ Vals ){
  15. $ Keysort [] = $ key;
  16. }
  17. $New_array= Array ();
  18. If ($ type! = "Asc "){
  19. For ($Ii=Count($ Keysort)-1; $ ii>= 0; $ ii --){
  20. $ New_array [] = $ arr [$ keysort [$ ii];
  21. }
  22. } Else {
  23. For ($Ii=0; $ Ii<Count($ Keysort); $ ii ++ ){
  24. $ New_array [] = $ arr [$ keysort [$ ii];
  25. }
  26. }
  27. Return $ new_array;
  28. }
  29. ?>

Bytes. PHP, an abbreviated name for nesting, is an English super Text preprocessing language (PHP: Hy...

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.