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

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

王林
Release: 2023-02-26 19:12:01
Original
3385 people have browsed it

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']; 
} 
?>
Copy after login

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!

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