SQL结果PHP多维数组 [英] SQL result to PHP multidimensional array

查看:161
本文介绍了SQL结果PHP多维数组的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从Oracle数据库使用功能通过连接检索某些分层数据。

I'm retrieving some hierarchical data from an Oracle database using the "connect by" function.

然后我填充我的查询看起来像结果的PHP数组:

Then I populate a PHP array with the result of my query looking like:

while ($row_branches = oci_fetch_array($query_tree)) {
   $tree[] = array(
     'id' => $row_branches['ID']
   , 'parent' => $row_branche['PARENT']
   , 'data' => htmlspecialchars($row_branches['NAME'])
   , 'level' => $row_branches['LEVEL']
   );
}

本场ID是行的唯一ID
本场Parent是父的ID
现场数据是项目的名称
现场级是层次结构行的水平。

The field ID is the unique id of the row The field PARENT is the ID of the parent The field DATA is the name of the item The field LEVEL is the level of the row in the hierarchy.

我宁愿有一个多维数组,因为我的目标是使用PHP函数json_de code()。

I'd rather have a multidimensional array because my goal is to use the PHP function json_decode().

层次结构的深度是从来没有事先已知的。

The depth of the hierarchy is never known in advance.

所以我的问题是:

我怎么能填充我的查询结果的多维数组?

How could I populate a multidimensional array with the result of my query?

太感谢了提前为你的答案。

Thanks a million in advance for your answers.

推荐答案

试试这个

function adj_tree(&$tree, $item) {
    $i = $item['ID'];
    $p = $item['PARENT'];
    $tree[$i] = isset($tree[$i]) ? $item + $tree[$i] : $item;
    $tree[$p]['_children'][] = &$tree[$i];
}

$tree = array();
while ($row = oci_fetch_array($query_tree)) {
   adj_tree($tree, $row);

这篇关于SQL结果PHP多维数组的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆