PHP Log Group sort code sharing

Source: Internet
Author: User
Tags foreach array sort sort sorts

  This article mainly introduces the PHP array sorting code, the need for friends can refer to the following

  Code below: <?php   class= ' pingjiaf ' frameborder= ' 0 ' src= ' http://www.jb51.net ' scrolling= ' no ' >   array sort The   Usort () function sorts the array using the user-defined function.  /    function cmp ($a, $b)//user-defined callback function   {  if ($a = = $b)//If two parameters equal   {  return 0;/ /back to 0  }   return ($a > $b) -1:1; If the 1th parameter is greater than 2nd returns 1, otherwise-1  }   $a =array (3,2,5,6,1); Defines an array   usort ($a, "CMP"); Use custom functions to sort the array   foreach ($a as $key => $value)///Cyclic output sorted key value pairs   {  echo "$key: $valuen";  }  /*   NOTE: If two elements have the same result, their order in the sorted array is undefined. User-defined functions will retain the original order of these elements before 4.0.6 to PHP. But as a result of the introduction of a new sorting algorithm in 4.1.0, this is not the case because there is no effective solution to this.    /   //Log Group Key name sort Uksort (array,sorttype)   function cmp ($a, $b)//user custom callback function   {  IF ($a = = $b)///If two parameters equal   {  return 0;//back 0  }   returns ($a > $b)? -1:1; If the 1th parameter is greater than 2nd returns 1, otherwise-1  }   $a =array (4=> "Four", 3 => "three", => "twenty",10=> "ten"); Define an array   UKSort ($a, "CMP"); Use custom functions to sort the array of key names   foreach ($a as $key => $value)///Cyclic output sorted key value pairs   {//www.jbxue.com   echo "$key: $valuen"; The  }/*  uksort () function uses a user-defined comparison function to sort by an array of key names and maintains an indexed relationship.     Returns True if successful, otherwise returns false.     If the array you want to sort needs to be sorted with an unusual standard, you should use this function.       Custom functions should accept two parameters, which will be populated by a pair of key names in the array. The comparison function must return an integer less than 0, equal to zero, or greater than 0, when the first argument is less than, equal to, or greater than the second argument.    /      /*  the sort () function sorts the values of the given array in ascending order.     NOTE: This function assigns a new key name to the cell in the array. The original key name was deleted.     Returns True if successful, otherwise returns false.    /    $fruits =array ("Lemon", "orange", "banana", "apple"); Defines an array   sort ($fruits); The array is sorted   foreach ($fruits as $key => $val)////loop outputs the sorted key value pair   {  echo "$key = $valn";//output key value to  } &NB Sp

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.