After a few hours, finally the process steps based on the sequence of steps to get it done.
Specific as follows:
1. Get a type of process step data from a database ...
But this data has the possibility of a dead loop, because the time to add is not very good judgment, but it is certainly impossible to make the next step for themselves.
2. You can know where the entrance to the process is, that is, what the ID of the first step looks like.
3. The ID order of the process is disrupted, which means that you cannot directly loop through the values by ID.
Solution Ideas
1. Known database content $ A, and the first step is 1, to determine whether there is a next step, did not directly return to the first steps;
2. Tidy $ A and let $ A first key be 1, which means the first key is the first step.
3. Generate a $ A value array and extract the number (the key value here cannot be duplicated)
4. Generate an array of processes to return the first key is the first step
5. Set the temporary value as the first step, for the Loop key value array is the judgment to prepare.
6. Array of loop key values
7, the loop starts with the second key and finds the next position in the current array of key values.
8. Judge to find the next step, and set the temporary value to the next value, generate the first key to return to the process array is the first step, here is the first step of rewriting, followed by the loop is appended
9. Swap the next value with the next loop starting bit of the array of key values, guaranteeing that the next loop is starting from the next first step.
Currently tested in the following cases, will not error, but the code seems a little bit long, or there is no shortcut to find.
1. Dead loop, 2. Database content in a disorderly order, 3. The different keys correspond to the same next step.
The detailed code is as follows:
<?PHP$a=Array( 7 = 7, 1 = 7, 8 = 9, 8 = 8, ten-one, one-and-one, one-and-one, oneNULL,);Var_dump($a);$firstID= 1;$nextID= 11;//here the program can find is 11//will be the first bit ahead$newReturnTemp[$firstID] =$nextID;if(Empty($nextID)) { return Array($a[$firstID]);} Else { //re-assemble the array and let start as the first bit foreach($a as $key=$value) { $newReturnTemp[$key] =$value; } unset($key); unset($value); //get the number of arrays $stepCount=Count($a); //extract all key values $stepArray=Array_keys($newReturnTemp); //Let the judgment bit be the first place $temp=$firstID; //the first bit of the array to be returned by the component $newnew[$firstID] =$nextID; //To loop the key values for($i= 0;$i<$stepCount;$i++) { //The default starts with the first step, if($temp==$stepArray[$i]) { //value following the array loop of the key value for($j=$i+ 1;$j<$stepCount;$j++) { //determine if the next level of value is found, based on the contents of $ A if($newReturnTemp[$stepArray[$i]] ==$stepArray[$j]) { //find the value, write the array that needs to be returned $newnew[$temp] =$stepArray[$j]; //set the next judgment value $temp=$stepArray[$j]; //swap the next one of the key-value loops with the next-level value that is currently returned, and ensure that the next judgment starts at the next level $tt=$stepArray[$j]; $stepArray[$j] =$stepArray[$i+ 1]; $stepArray[$i+ 1] =$tt; unset($tt); } } } } //The sorted array is returned and the corresponding data is taken out at once based on the queued content, here is a demo with a $ A ... foreach($newnew as $key=$value) { $reReturn[$key] =$a[$key]; $last=$value; } //will default to the last not found value or is incorrect next level to fill in the array ... (This step may not be required) if(isset($a[$last])) { $reReturn[$last] =$a[$last]; }}Var_dump($reReturn);