Detailed explanation of PHP sorting algorithm examples

墨辰丷
Release: 2023-03-28 18:16:01
Original
1288 people have browsed it

This article mainly introduces the PHP sorting algorithm, and analyzes the related operating skills and precautions of PHP data query, sorting, array deduplication, traversal and sorting in the form of examples. Friends in need can refer to the following

Use PHP to write sorting. Although PHP automatically automates many sorting methods, SQL statements can also quickly read data in order from the database. But different needs still require flexible application of the basic knowledge of PHP learned.

I want to achieve the following effect

Sorting algorithm effect diagram

It is to sort all the data in a value by time and divide it into rows Display the

<?php
  $array = $mysql->query_array($mysql->sql_select("user","userid,truename,year"," ")); //从数据库里面读出数据
  $year = array(); //设置空数组
  foreach($array as $value)
  {
    array_push($year,$value[2]); //把数据数组里面的时间数据入栈
  }
  $year = array_unique($year); //去除数组中重复的数据
  usort($year, "strnatcmp"); //将数组中的数据按自然数排序
  $num = count($year); //统计数据中数据的个数
 /*处理函数*/
 function isyear($value,$array)
 {
   $user = array(); //建立空数组
   foreach($array as $val)
   {
    if($val[2] == $value)
    {
     array_push($user,$val);
    }
   }
   return $user; //返回数组
 }
?>
Copy after login

above word processing function. The following is the display part of the code

<?php
for($i=0;$i<$num;$i++)
{
echo "<p class=&#39;box_user&#39;>";
echo "<p class=&#39;boxleft&#39;>{$year[$i]}届</p>";
echo  "<p class=&#39;boxright&#39;>";
$user = isyear($year[$i],$array);
foreach($user as $v)
{
echo "<span class={$v[0]}>{$v[1]}</span>";
}
echo "</p>";
echo "</p>";
}
?>
Copy after login

The data display code should be easy to understand, so I will not write comments line by line here.

This way we can get the effect we need to start with. Don't think that's the end of it. . This is just a first step idea, abstracting the actual problem into code. I don’t know what the effect will be?

We can use timestamps to calculate the time it takes to load page data and use PHP automatic functions.

<?php
  memory_get_usage();
?>
Copy after login

To calculate the memory occupied by the code while it is running.

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

Related recommendations:

Example of how PHP uses curl_multi to implement concurrent requests


Installation and use of PHP performance testing tool xhprof Detailed explanation of the method


PHP method of locating the cause of the fault through strace


##

The above is the detailed content of Detailed explanation of PHP sorting algorithm examples. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
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!