Как я могу преобразовать таблицу базы данных следующим образом:
Я попробовал рекурсивный цикл, как показано ниже, но не может отобразить его правильно.
function cost_centres_format($items) { foreach ($items as $item) { echo $item->name.' - '.$item->parent_id; echo '<br/>'; $sons = $this->purchase_order_model->get_cost_centre_sons($item->internal_purchase_order_cost_centre_id); if(count($sons)>0){ $this->cost_centres_format($sons); } } }
Вот мое решение для этого:
function cost_centres_format($items,$parent_id,$array=array()) { foreach($items as $item) { if($item->parent_id == $parent_id) { $array[] = $item; if($item->internal_purchase_order_cost_centre_id>0) { $array = cost_centres_format($items,$item->internal_purchase_order_cost_centre_id,$array); } } } return $array; } $array = cost_centres_format($items,0);
Дизель (id: 5) будет ниже Технического обслуживания транспортного средства (id: 4) из-за его первоначального заказа. Вы можете сделать дополнительный сортировать по имени, но в вашем примере Capital (id: 3) был ниже Overheads (id: 2).
Вышеприведенный код не создает многомерный массив, а вместо него один размерный массив:
Array ( [0] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 1 [name] => Direct Expenses [parent_id] => 0 ) [1] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 4 [name] => Vehicle Maintenance [parent_id] => 1 ) [2] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 9 [name] => CN09 AKO [parent_id] => 4 ) [3] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 10 [name] => DY52 BYO [parent_id] => 4 ) [4] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 14 [name] => MX08 MVJ [parent_id] => 4 ) [5] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 15 [name] => YJ55 TXA [parent_id] => 4 ) [6] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 5 [name] => Diesel [parent_id] => 1 ) [7] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 6 [name] => Vehicle Rent [parent_id] => 1 ) [8] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 11 [name] => Vehicle Repair [parent_id] => 1 ) [9] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 16 [name] => CN09 AKO [parent_id] => 11 ) [10] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 17 [name] => DY52 BYO [parent_id] => 11 ) [11] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 18 [name] => MX08 MVJ [parent_id] => 11 ) [12] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 19 [name] => YJ55 TXA [parent_id] => 11 ) [13] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 2 [name] => Overheads [parent_id] => 0 ) [14] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 12 [name] => Internet Service [parent_id] => 2 ) [15] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 13 [name] => Warehouse Rent [parent_id] => 2 ) [16] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 3 [name] => Capital [parent_id] => 0 ) [17] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 7 [name] => New Stock [parent_id] => 3 ) [18] => stdClass Object ( [internal_purchase_order_cost_centre_id] => 8 [name] => New Vehicle [parent_id] => 3 )
)