PHP   发布时间:2022-04-04  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了PHP从平面数据集创建多维对象(来自Mysql表)大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

这是mysql表中的数据集:

使用MySQL嵌套集模型并不明显,因为我省略了lft和rgt列.

+------+----------+--------------------------------+-------+
+ Id   | ParentId | Name                           | Level |
+------+----------+--------------------------------+-------+
| 1001 |     NULL | Computing                      |     0 |
| 1002 |     NULL | Cassettes & Vinyl              |     0 |
| 1003 |     1001 | CD Players                     |     1 |
| 1004 |     1002 | CD Writers                     |     1 |
| 1005 |     1003 | CD-ROM Drives                  |     2 |
| 1006 |     1004 | CDs                            |     2 |
+------+----------+--------------------------------+-------+

这被拉入二维数组:

<?PHP
$data = array(
          array('id' => 1001, 'ParentId' => NULL, 'Name' => 'Computing', 'Level' => 0),
          array('id' => 1002, 'ParentId' => NULL, 'Name' => 'Cassettes & Vinyl', 'Level' => 0),
          array('id' => 1003, 'ParentId' => 1001, 'Name' => 'CD Players', 'Level' => 1),
          array('id' => 1004, 'ParentId' => 1002, 'Name' => 'CD Writers', 'Level' => 1),
          array('id' => 1005, 'ParentId' => 1003, 'Name' => 'CD-ROM Drives', 'Level' => 2),
          array('id' => 1006, 'ParentId' => 1004, 'Name' => 'Computing', 'Level' => 3)
        );
?>

我正在尝试实现以下多维对象:

stdClass Object {
    [0] => stdClass Object {
        [id] => 1001
        [name] => Computing
        [parentId] => NULL
        [level] => 0
        [children] => stdClass Object {
            [0] => stdClass Object {
                [id] => 1003
                [name] => CD Players
                [parentId] => 1001
                [level] => 1
                [children] => stdClass Object {
                    [0] => stdClass Object {
                        [id] => 1005
                        [name] => CD Rom Drives
                        [parentId] => 1003
                        [level] => 2
                    }
                }
            }
        }
    }
    [1] => stdClass Object {
        [id] => 1002
        [name] => Cassettes & Vinyl
        [parentId] => NULL
        [level] => 0
        [children] => stdClass Object {
            [0] => stdClass Object {
                [id] => 1004
                [name] => CD Writers
                [parentId] => 1002
                [level] => 1
                [children] => stdClass Object {
                    [0] => stdClass Object {
                        [id] => 1006
                        [name] => CDs
                        [parentId] => 1004
                        [level] => 2
                    }
                }
            }
        }
    }
}

我一直在玩递归函数,运气不佳.

为什么? -此对象将用于创建Zend_Navigation.我不想在此阶段使用XML文件,我希望分类管理在数据库中完成.

有任何想法吗?

解决方法:

这不一定是递归的工作,可以通过迭代轻松完成.

<?PHP

$data = array(
  array('id' => 1001, 'ParentId' => NULL, 'Name' => 'Computing', 'Level' => 0),
  array('id' => 1002, 'ParentId' => NULL, 'Name' => 'Cassettes & Vinyl', 'Level' => 0),
  array('id' => 1003, 'ParentId' => 1001, 'Name' => 'CD Players', 'Level' => 1),
  array('id' => 1004, 'ParentId' => 1002, 'Name' => 'CD Writers', 'Level' => 1),
  array('id' => 1005, 'ParentId' => 1003, 'Name' => 'CD-ROM Drives', 'Level' => 2),
  array('id' => 1006, 'ParentId' => 1004, 'Name' => 'Computing', 'Level' => 3)
);

$index = array();
$tree  = array();

// step 1: build index (note that I use &$row references!) 
foreach ($data as &$row) {
  $index[$row['id']] = &$row;
  $row['children'] = array();
  if ($row['ParentId'] == NULL) {
    $tree[] = &$row;
  }
}

// step 2: link tree (references here as well!)
foreach ($data as &$row) {
  $index[$row['ParentId']]['children'][] = &$row;
}

print_r($tree);

?>

结果:

Array
(
  [0] => Array
  (
    [id] => 1001
    [ParentId] => 
    [Name] => Computing
    [Level] => 0
    [children] => Array
    (
      [0] => Array
      (
        [id] => 1003
        [ParentId] => 1001
        [Name] => CD Players
        [Level] => 1
        [children] => Array
        (
          [0] => Array
          (
            [id] => 1005
            [ParentId] => 1003
            [Name] => CD-ROM Drives
            [Level] => 2
            [children] => Array
            (
            )
          )
        )
      )
    )
  )
  [1] => Array
  (
    [id] => 1002
    [ParentId] => 
    [Name] => Cassettes & Vinyl
    [Level] => 0
    [children] => Array
    (
      [0] => Array
      (
        [id] => 1004
        [ParentId] => 1002
        [Name] => CD Writers
        [Level] => 1
        [children] => Array
        (
          [0] => Array
          (
            [id] => 1006
            [ParentId] => 1004
            [Name] => Computing
            [Level] => 3
            [children] => Array
            (
            )
          )
        )
      )
    )
  )
)

大佬总结

以上是大佬教程为你收集整理的PHP从平面数据集创建多维对象(来自Mysql表)全部内容,希望文章能够帮你解决PHP从平面数据集创建多维对象(来自Mysql表)所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: