Home  >  Article  >  Backend Development  >  What should I do if PHP recursive infinite classification is slow?

What should I do if PHP recursive infinite classification is slow?

王林
王林Original
2019-10-15 17:55:013195browse

What should I do if PHP recursive infinite classification is slow?

1. First decode json into an array, use the json_decode function. Note that you must add the second parameter, otherwise it will return an object. The next step is recursion. This is the simplest recursion that just needs to be traversed one by one.

What should I do if PHP recursive infinite classification is slow?

2. The following is the complete code:

$data= json_decode($str,true); 
$options = getChildren($data); 
function getChildren($parent,$deep=0) { 
foreach($parent as $row) { 
$data[] = array("id"=>$row['id'], "name"=>$row['name'],"pid"=>$row['parentid'],'deep'=>$deep); 
if ($row['childs']) { 
$data = array_merge($data, getChildren($row['childs'], $deep+1)); 
} } 
return $data; 
} 
foreach ($options as $row) { 
echo str_pad("",$row['deep']*3, "-",STR_PAD_RIGHT); 
echo $row['name']; 
} 
?>

Recommended tutorial: PHP video tutorial

The above is the detailed content of What should I do if PHP recursive infinite classification is slow?. 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