Php infinite cycle

how to obtain the following data

{

title=titlevalue=iddata= pid=id

pid=0
title=value=1,data=[]

}

Php
Mar.05,2021

function tree($arr, $pid=0){
    foreach($arr as $k => $v){
        if($v['pid'] == $pid){
            $v['data'] = tree($arr, $v['id']);
            $tree[] = $v;
        }
    }
    return isset($tree)? $tree : [];
}
Menu