$column) { $object[$column] = $record[$index]; foreach ($tables as $relation=>$reltable) { if (isset($reltable['relations'])) { foreach ($reltable['relations'] as $key=>$target) { if ($target == "$table_name.$column") { $column_indices = array_flip($reltable['columns']); $object[$relation] = $get_objects($tables,$relation,$column_indices[$key],$record[$index]); } } } } } $objects[] = $object; } } } return $objects; }; $tree = array(); foreach ($tables as $name=>$table) { if (!isset($table['relations'])) { $tree[$name] = $get_objects($tables,$name); if (isset($table['results'])) { $tree['_results'] = $table['results']; } } } return $tree; }