Home >Backend Development >PHP Tutorial >How to implement combined sorting in php

How to implement combined sorting in php

墨辰丷
墨辰丷Original
2018-06-01 09:07:401505browse

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 the following

The example of this article describes the simple implementation method of PHP combination sorting. I would like to share it with you for your reference. The details are as follows:

I was struggling with a combination of sorting all night today. Maybe I didn’t turn 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, I am struggling to death. First, use sql to combine the query. The result is combined, but the effect is 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; //返回学生信息节点
}

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"
 }
}

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:

phpGenerating and reading excel files_phpExamples

phpImplementation of determining client type

Detailed explanation of PHP file upload processing case

The above is the detailed content of How to implement combined sorting in php. For more information, please follow other related articles on the PHP Chinese website!

Statement:
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