function conn(){
try{
$pdo = new PDO("mysql:host=127.0.0.1;dbname=test",'root','root');
$pdo->exec("set names utf8");
return $pdo;
}catch(PDOException $ex){
$ex->getMessage();
}
}
function getlist($pid = 0,$result = [],$num = 0){
$num++;
$pdo = conn();
$sql = "select * from cate where pid = " . $pid;
$res = $pdo->prepare($sql);
$res->execute();
while($data = $res->fetch(PDO::FETCH_ASSOC)){
static $result;
$data['catename'] = str_repeat("|—",$num) . $data['catename'];
$result[] = $data;
getlist($data['id'],$result,$num);
}
return $result;
}
$res = getlist();
echo "<pre/>";
print_r($res);