Getting all category arranged in php

<?php

$categories = getTableData("categories", "*", "pid=0 LIMIT 2",TRUE);


//Getting sub category under this main category::

foreach($categories as $key => &$val) {

$val['sub'] = getTableData( "categories","*","pid={$val['id']}", TRUE);

if(empty($val['sub'])) unset($categories[$key]);

}


print_r($categories);
echo "</pre>";