How to properly sort MPTT hierarchy data into multidimensional array ?

Posted by DiegoMax on Stack Overflow See other posts from Stack Overflow or by DiegoMax
Published on 2009-12-21T14:05:47Z Indexed on 2010/03/24 4:03 UTC
Read the original article Hit count: 227

Helo there, im trying to figure out how to write a function that returns a multidimensional array, based on the data below:

I know how to write the function using the "category_parent" value, but im just trying to write a function that can create a multidimensional array by JUST using the left and right keys.

Any help greatly appreciated!

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"
  }
}

© Stack Overflow or respective owner

Related posts about mptt

Related posts about php