Как сортировать массив как дерево (с родительскими объектами)?


У меня есть sig array, где:

  • Id является основным id.

  • Id_padre указывает, что является родителем этого объекта.

array:26 [▼
  0 => array:5 [▼
    "id" => 1
    "icon" => "box-open"
    "text" => "Caja"
    "url" => null
    "id_padre" => 0
  ]
  1 => array:5 [▼
    "id" => 2
    "icon" => "box-open"
    "text" => "Caja"
    "url" => "caja"
    "id_padre" => 1
  ]
  2 => array:5 [▼
    "id" => 3
    "icon" => "scroll"
    "text" => "Vale - Ingresos y Egresos"
    "url" => "vale"
    "id_padre" => 1
  ]
  3 => array:5 [▼
    "id" => 4
    "icon" => "shopping-cart"
    "text" => "Compra"
    "url" => "compra"
    "id_padre" => 1
  ]
  4 => array:5 [▼
    "id" => 5
    "icon" => "money-check"
    "text" => "Arqueo"
    "url" => "arqueo"
    "id_padre" => 1
  ]
  5 => array:5 [▼
    "id" => 6
    "icon" => "exchange-alt"
    "text" => "Movimientos"
    "url" => null
    "id_padre" => 0
  ]
  6 => array:5 [▼
    "id" => 7
    "icon" => "shopping-cart"
    "text" => "Pedido"
    "url" => null
    "id_padre" => 6
  ]
  7 => array:5 [▼
    "id" => 8
    "icon" => "shopping-cart"
    "text" => "Ventas"
    "url" => "pedido_tab"
    "id_padre" => 7
  ]
  8 => array:5 [▼
    "id" => 9
    "icon" => "shopping-cart"
    "text" => "Historial"
    "url" => "pedido"
    "id_padre" => 7
  ]
  9 => array:5 [▼
    "id" => 10
    "icon" => "people-carry"
    "text" => "Traslado"
    "url" => "traslado"
    "id_padre" => 7
  ]
  10 => array:5 [▼
    "id" => 11
    "icon" => "carrot"
    "text" => "Insumo"
    "url" => null
    "id_padre" => 6
  ]
  11 => array:5 [▼
    "id" => 12
    "icon" => "tasks"
    "text" => "Gestion"
    "url" => "insumo"
    "id_padre" => 11
  ]
  12 => array:5 [▼
    "id" => 13
    "icon" => "carrot"
    "text" => "Categoria"
    "url" => "insumo_categoria"
    "id_padre" => 11
  ]
  13 => array:5 [▼
    "id" => 14
    "icon" => "exchange-alt"
    "text" => "Almacen"
    "url" => null
    "id_padre" => 0
  ]
  14 => array:5 [▼
    "id" => 15
    "icon" => "drumstick-bite"
    "text" => "Productos"
    "url" => null
    "id_padre" => 14
  ]
  15 => array:5 [▼
    "id" => 16
    "icon" => "tasks"
    "text" => "Gestion"
    "url" => "producto"
    "id_padre" => 15
  ]
  16 => array:5 [▼
    "id" => 17
    "icon" => "drumstick-bite"
    "text" => "Categoria"
    "url" => "producto_categoria"
    "id_padre" => 15
  ]
  17 => array:5 [▼
    "id" => 18
    "icon" => "box"
    "text" => "Almacen"
    "url" => "almacen"
    "id_padre" => 14
  ]
  18 => array:5 [▼
    "id" => 19
    "icon" => "box"
    "text" => "Inventario"
    "url" => "inventario"
    "id_padre" => 14
  ]
  19 => array:5 [▼
    "id" => 20
    "icon" => "wrench"
    "text" => "Configuracion"
    "url" => null
    "id_padre" => 0
  ]
  20 => array:5 [▼
    "id" => 21
    "icon" => "store"
    "text" => "Local"
    "url" => "local"
    "id_padre" => 20
  ]
  21 => array:5 [▼
    "id" => 22
    "icon" => "store"
    "text" => "Ambiente"
    "url" => "ambiente"
    "id_padre" => 20
  ]
  22 => array:5 [▼
    "id" => 23
    "icon" => "people-carry"
    "text" => "Proveedor"
    "url" => "proveedor"
    "id_padre" => 20
  ]
  23 => array:5 [▼
    "id" => 24
    "icon" => "infinity"
    "text" => "Unidad de Medida"
    "url" => "unidad_medida"
    "id_padre" => 20
  ]
  24 => array:5 [▼
    "id" => 25
    "icon" => "chart-pie"
    "text" => "Reportes"
    "url" => "reporte"
    "id_padre" => 0
  ]
  25 => array:5 [▼
    "id" => 26
    "icon" => "chart-area"
    "text" => "Estadistica"
    "url" => "estadi"
    "id_padre" => 25
  ]
]
Author: Shaz, 2019-02-27

1 answers

Вы можете сделать это следующим образом:

// funcion find father recibe el arreglo y el id por defecto 0
function ffather($arr,$el=0){
    // creamos una varible final que contendra nuestros hijos
    $final=array();
    // recorremos el arreglo
    foreach ($arr as $key => $value) {
        // validamos que el el id actual coincida con el id_padre "encontramos un hijo"
        if ($el == $value["id_padre"]){
            // volvemos a llamar a la funcion find father para buscar ahora los hijos de los hijos
            $value["hijos"]=ffather($arr,$value["id"]);
            // cargamos el nuevo valor en $final
            $final[]= $value;
        }
    }
    // retornamos $final
    return $final;
}

echo "<pre>";
print_r(ffather($arr));
 2
Author: Bryro, 2019-02-27 20:42:06