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.
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!