PHP method to implement array sorting based on ArraySortUtil

墨辰丷
Release: 2023-03-31 16:06:02
Original
1251 people have browsed it

This article mainly introduces the usage of PHP's special array sorting class ArraySortUtil. The example analyzes ArraySortUtil's method of implementing array sorting and the corresponding usage skills. Friends in need can refer to it.

This article describes the PHP special array with examples. Sorting class ArraySortUtil usage. The details are as follows:

**
 * ArraySortUtil is a array sort utility, you can extends the sorting engine.
 *
 * @version 0.1
 * @package utility.sort
 */
class ArraySortUtil
{
 static function uasort($unsort, $fields)
 {
  if ( !is_array($unsort) || sizeof($unsort) <= 0 ) return $unsort;
  $sorted = uasortEngine::uasort($unsort, $fields);
  return $sorted;
 }
 static function multisort($unsort, $fields)
 {
  if ( !is_array($unsort) || sizeof($unsort) <= 0 ) return $unsort;
  $sorted = multisortEngine::multisort($unsort, $fields);
  return $sorted;
 }
}
class multisortEngine
{
 static function multisort($unsort, $fields)
 {
  $sorted = $unsort;
  if (is_array($unsort))
  {
   $loadFields = array();
   foreach($fields as $sortfield)
   {
    $loadFields["field"][] = array(
        "name" => $sortfield["field"],
        "order" => $sortfield["order"],
        "nature" => $sortfield["nature"],
        "caseSensitve" => $sortfield["caseSensitve"]
    );
    $loadFields["data"][$field["field"]] = array();
   }
   // Obtain a list of columns
   foreach ($sorted as $key => $row) {
    foreach($loadFields["field"] as $field) {
     $value = $row[$field["name"]];
     $loadFields["data"][$field["name"]][$key] = $value;
    }
   }
   $parameters = array();
   foreach($loadFields["field"] as $sortfield) {
    $array_data = $loadFields["data"][$sortfield["name"]];
    $caseSensitve = ( $sortfield["caseSensitve"] == null ) ? $sortfield["caseSensitve"] : false;
    if (!$caseSensitve) $array_data = array_map(&#39;strtolower&#39;, $array_data);
    $parameters[] = $array_data;
    if ( $sortfield["order"] != null ) $parameters[] = ( $sortfield["order"] ) ? SORT_DESC : SORT_ASC;
    if ( $sortfield["nature"] != null ) $parameters[] = ( $sortfield["nature"] ) ? SORT_REGULAR : SORT_STRING;
   }
   $parameters[] = &$sorted;
   call_user_func_array("array_multisort", $parameters);
  }
  return $sorted;
 }
}
class uasortEngine
{
 static private $caseSensitve = false;
 static private $sortfields = array();
 static private $sortorder = true;
 static private $nature = false;
 static private function uasort_callback(&$a, &$b)
 {
  foreach(self::$sortfields as $sortfield)
  {
   $_field = $sortfield["field"];
   $_order = isset($sortfield["order"]) ? $sortfield["order"] : self::$sortorder;
   $_caseSensitve = isset($sortfield["caseSensitve"]) ? $sortfield["caseSensitve"] : self::$caseSensitve;
   $_nature = isset($sortfield["nature"]) ? $sortfield["nature"] : self::$nature;
   if ($_field != "")
   {
    $retval = 0;
    if ($_nature)
    {
     if ($_caseSensitve)
     {
      $compare = strnatcmp($a[$_field], $b[$_field]);
     }
     else
     {
      $compare = strnatcasecmp($a[$_field], $b[$_field]);
     }
    }
    else
    {
     if ($_caseSensitve)
     {
      $compare = strcmp($a[$_field], $b[$_field]);
     }
     else
     {
      $compare = strcasecmp($a[$_field], $b[$_field]);
     }
    }
    if ($compare !== 0 && !$_order) $compare = ($compare > 0) ? -1 : 1;
   }
   if ($compare !== 0) break;
  }
  return $compare;
 }
 static function uasort($unsort, $fields)
 {
  self::$sortfields = $fields;
  $sorted = $unsort;
  uasort($sorted, array(&#39;uasortEngine&#39;, &#39;uasort_callback&#39;));
  return $sorted;
 }
}
Copy after login

Usage examples are as follows:

<?php
require_once(&#39;class.ArraySortUtil.php&#39;);
// declare test data
$assetData[] = array("id" => 1, "category"=>"Hardware", "subcategory"=>"Personal Computer", "supplier" => "DELL", "particular"=>"Vostro 1320", "purchase_price"=>2300, "other_charges"=>500);
$assetData[] = array("id" => 2, "category"=>"Hardware", "subcategory"=>"Personal Computer", "supplier" => "DELL", "particular"=>"Vostro 1420", "purchase_price"=>2500, "other_charges"=>0);
$assetData[] = array("id" => 3, "category"=>"Hardware", "subcategory"=>"Laptop", "supplier" => "DELL", "particular"=>"Vostro 1520", "purchase_price"=>4500, "other_charges"=>100);
$assetData[] = array("id" => 4, "category"=>"Hardware", "subcategory"=>"Laptop", "supplier" => "Acer", "particular"=>"Apire One", "purchase_price"=>2500, "other_charges"=>200);
$assetData[] = array("id" => 5, "category"=>"Furniture", "subcategory"=>"Table", "supplier" => "CHEN", "particular"=>"Manager Table", "purchase_price"=>1000, "other_charges"=>0);
$assetData[] = array("id" => 6, "category"=>"Furniture", "subcategory"=>"Table", "supplier" => "CHEN", "particular"=>"Staff Table", "purchase_price"=>500, "other_charges"=>0);
$assetData[] = array("id" => 7, "category"=>"Furniture", "subcategory"=>"Chair", "supplier" => "CHEN", "particular"=>"Staff Chair", "purchase_price"=>500, "other_charges"=>0);
$sorted = ArraySortUtil::multisort($assetData, array(
  array("field"=>"category"),
  array("field"=>"subcategory", "order" => true) // desc
 ));
echo "<h1>multisort:result</h1>";
echo "<pre class="brush:php;toolbar:false">";
 print_r($sorted);
echo "
"; $sorted = ArraySortUtil::uasort($assetData, array( array("field"=>"category"), array("field"=>"subcategory", "order" => true) // desc )); echo "

uasort:result

"; echo "
";
 print_r($sorted);
echo "
";
Copy after login

Summary: The above is the entire content of this article, I hope it will be helpful to everyone's study.

Related recommendations:

PHP implementation of Chinese character verification code

PHP implementation of unordered tree Skills

#php process control and mathematical operations

##

The above is the detailed content of PHP method to implement array sorting based on ArraySortUtil. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
php
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!