Home > Backend Development > PHP Tutorial > A simple method to implement combined sorting in PHP

A simple method to implement combined sorting in PHP

墨辰丷
Release: 2023-03-28 18:44:02
Original
1343 people have browsed it

This article mainly introduces the simple implementation method of PHP combination sorting, involving PHP data structure and mathematical operations related operation skills. Friends in need can refer to it

I was entangled in a combination sorting all night today, maybe I had never turned a corner at first, so I didn’t think of using two stacks. Using two stacks, it is very easy to complete the required effect

combination sorting imagination diagram

In order to complete this effect diagram, it can be tangled to death I guess, I first used sql to combine the query, and the result was combined, but the effect was not achieved. Now post the PHP code

//获取学生信息
private function ground($data)
{
$stu = array(); //新建一个学号栈,存储学生学号
foreach($data as $key=>$value)
{
if(in_array($value["studentid"],$stu)) //判断学号是不是已经被存储
{
array_push($array[$value["studentid"]],$value["selectbh"]); //入栈
array_push($array[$value["studentid"]],$value["taskid"]); //入栈
}
else
{ // 动态新建学生信息栈。
$array[$value["studentid"]] = array($value['selectbh'],$value['taskid']);
array_push($stu,$value["studentid"]); //再学号栈里面没有的学号入栈
}
}
unset($stu); //注销学号栈
return $array; //返回学生信息节点
}
Copy after login

The above code can achieve the effect we want. Preview this page to get the following effect

array(2) {
 ["10408400227"] => array(4) {
  [0] => string(1) "1"
  [1] => string(1) "4"
  [2] => string(1) "2"
  [3] => string(1) "5"
 }
 ["10408400229"] => array(6) {
  [0] => string(1) "1"
  [1] => string(1) "4"
  [2] => string(1) "2"
  [3] => string(2) "28"
  [4] => string(1) "3"
  [5] => string(2) "14"
 }
}
Copy after login

A two-dimensional array is formed, and the student number is used as the key of the array. In this way, the student number is associated, and the corresponding rendering can be obtained.

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

Related recommendations:

PHP’s coupled design pattern

##Detailed explanation of PHP reference variable knowledge

PHP Common Function Collection

The above is the detailed content of A simple method to implement combined sorting in PHP. 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