如何将 MPTT 层次结构数据正确排序到多维数组中。

发布于 2024-08-15 08:11:33 字数 4211 浏览 8 评论 0原文

我试图弄清楚如何编写一个返回多维数组的函数。我知道如何使用“category_parent”值编写函数。但我只是想编写一个仅使用左右键即可创建多维数组的函数。

array(71) {
  [0]=>
  array(9) {
    ["id"]=>
    string(1) "1"
    ["category_name"]=>
    string(6) "Rubros"
    ["category_parent"]=>
    string(1) "0"
    ["category_slug"]=>
    string(6) "rubros"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    NULL
    ["category_lft"]=>
    string(1) "1"
    ["category_rgt"]=>
    string(3) "142"
  }
  [1]=>
  array(9) {
    ["id"]=>
    string(4) "1000"
    ["category_name"]=>
    string(12) "Restaurantes"
    ["category_parent"]=>
    string(1) "1"
    ["category_slug"]=>
    string(12) "restaurantes"
    ["category_image"]=>
    string(16) "restaurantes.png"
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "2"
    ["category_rgt"]=>
    string(2) "13"
  }
  [2]=>
  array(9) {
    ["id"]=>
    string(1) "3"
    ["category_name"]=>
    string(21) "Restaurantes de Campo"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(21) "restaurantes-de-campo"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "3"
    ["category_rgt"]=>
    string(1) "4"
  }
  [3]=>
  array(9) {
    ["id"]=>
    string(2) "37"
    ["category_name"]=>
    string(25) "Restaurantes en la Ciudad"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(19) "restaurantes-ciudad"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "6"
    ["category_lft"]=>
    string(1) "5"
    ["category_rgt"]=>
    string(1) "6"
  }
  [4]=>
  array(9) {
    ["id"]=>
    string(2) "41"
    ["category_name"]=>
    string(21) "Servicios de Catering"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(8) "catering"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "7"
    ["category_rgt"]=>
    string(1) "8"
  }
  [5]=>
  array(9) {
    ["id"]=>
    string(2) "48"
    ["category_name"]=>
    string(10) "Rotiserias"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(10) "rotiserias"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "9"
    ["category_rgt"]=>
    string(2) "10"
  }
  [6]=>
  array(9) {
    ["id"]=>
    string(2) "62"
    ["category_name"]=>
    string(10) "Pizzerías"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(9) "pizzerias"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(2) "11"
    ["category_rgt"]=>
    string(2) "12"
  }
  [7]=>
  array(9) {
    ["id"]=>
    string(1) "2"
    ["category_name"]=>
    string(13) "Profesionales"
    ["category_parent"]=>
    string(1) "1"
    ["category_slug"]=>
    string(13) "profesionales"
    ["category_image"]=>
    string(17) "profesionales.png"
    ["category_totals"]=>
    string(1) "2"
    ["category_lft"]=>
    string(2) "14"
    ["category_rgt"]=>
    string(2) "35"
  }
  [8]=>
  array(9) {
    ["id"]=>
    string(2) "29"
    ["category_name"]=>
    string(11) "Arquitectos"
    ["category_parent"]=>
    string(1) "2"
    ["category_slug"]=>
    string(11) "arquitectos"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    string(1) "0"
    ["category_lft"]=>
    string(2) "15"
    ["category_rgt"]=>
    string(2) "16"
  }
  [9]=>
  array(9) {
    ["id"]=>
    string(2) "30"
    ["category_name"]=>
    string(8) "Abogados"
    ["category_parent"]=>
    string(1) "2"
    ["category_slug"]=>
    string(8) "abogados"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    string(1) "6"
    ["category_lft"]=>
    string(2) "17"
    ["category_rgt"]=>
    string(2) "18"
  }
}

任何帮助表示赞赏!

I'm trying to figure out how to write a function that returns a multidimensional array. I know how to write the function using the "category_parent" value. But I'm just trying to write a function that can create a multidimensional array by ONLY using the left and right keys.

array(71) {
  [0]=>
  array(9) {
    ["id"]=>
    string(1) "1"
    ["category_name"]=>
    string(6) "Rubros"
    ["category_parent"]=>
    string(1) "0"
    ["category_slug"]=>
    string(6) "rubros"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    NULL
    ["category_lft"]=>
    string(1) "1"
    ["category_rgt"]=>
    string(3) "142"
  }
  [1]=>
  array(9) {
    ["id"]=>
    string(4) "1000"
    ["category_name"]=>
    string(12) "Restaurantes"
    ["category_parent"]=>
    string(1) "1"
    ["category_slug"]=>
    string(12) "restaurantes"
    ["category_image"]=>
    string(16) "restaurantes.png"
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "2"
    ["category_rgt"]=>
    string(2) "13"
  }
  [2]=>
  array(9) {
    ["id"]=>
    string(1) "3"
    ["category_name"]=>
    string(21) "Restaurantes de Campo"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(21) "restaurantes-de-campo"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "3"
    ["category_rgt"]=>
    string(1) "4"
  }
  [3]=>
  array(9) {
    ["id"]=>
    string(2) "37"
    ["category_name"]=>
    string(25) "Restaurantes en la Ciudad"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(19) "restaurantes-ciudad"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "6"
    ["category_lft"]=>
    string(1) "5"
    ["category_rgt"]=>
    string(1) "6"
  }
  [4]=>
  array(9) {
    ["id"]=>
    string(2) "41"
    ["category_name"]=>
    string(21) "Servicios de Catering"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(8) "catering"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "7"
    ["category_rgt"]=>
    string(1) "8"
  }
  [5]=>
  array(9) {
    ["id"]=>
    string(2) "48"
    ["category_name"]=>
    string(10) "Rotiserias"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(10) "rotiserias"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(1) "9"
    ["category_rgt"]=>
    string(2) "10"
  }
  [6]=>
  array(9) {
    ["id"]=>
    string(2) "62"
    ["category_name"]=>
    string(10) "Pizzerías"
    ["category_parent"]=>
    string(4) "1000"
    ["category_slug"]=>
    string(9) "pizzerias"
    ["category_image"]=>
    string(0) ""
    ["category_totals"]=>
    string(1) "1"
    ["category_lft"]=>
    string(2) "11"
    ["category_rgt"]=>
    string(2) "12"
  }
  [7]=>
  array(9) {
    ["id"]=>
    string(1) "2"
    ["category_name"]=>
    string(13) "Profesionales"
    ["category_parent"]=>
    string(1) "1"
    ["category_slug"]=>
    string(13) "profesionales"
    ["category_image"]=>
    string(17) "profesionales.png"
    ["category_totals"]=>
    string(1) "2"
    ["category_lft"]=>
    string(2) "14"
    ["category_rgt"]=>
    string(2) "35"
  }
  [8]=>
  array(9) {
    ["id"]=>
    string(2) "29"
    ["category_name"]=>
    string(11) "Arquitectos"
    ["category_parent"]=>
    string(1) "2"
    ["category_slug"]=>
    string(11) "arquitectos"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    string(1) "0"
    ["category_lft"]=>
    string(2) "15"
    ["category_rgt"]=>
    string(2) "16"
  }
  [9]=>
  array(9) {
    ["id"]=>
    string(2) "30"
    ["category_name"]=>
    string(8) "Abogados"
    ["category_parent"]=>
    string(1) "2"
    ["category_slug"]=>
    string(8) "abogados"
    ["category_image"]=>
    NULL
    ["category_totals"]=>
    string(1) "6"
    ["category_lft"]=>
    string(2) "17"
    ["category_rgt"]=>
    string(2) "18"
  }
}

Any help is appreciated!

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

哎呦我呸! 2024-08-22 08:11:33

你这样做就像从文件中分离数据一样,就像这样 '["dimensionOne"]["dimensionTwo"]'

You do it like you're pulling apart data from a file, like so '["dimensionOne"]["dimensionTwo"]'

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文