1

How can I convert a database table like that:

enter image description here

into a multi-dimensional array like that ?

enter image description here

I tried a recursive loop like below but can't get it to display properly.

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);
        }
    }
}

2 Answers 2

0

Here is my solution for it:

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);

Diesel (id:5) will be below Vehicle Maintenance (id:4) because of its original order. You can do an additional sort by name but in your example Capital (id:3) was below Overheads (id:2).

Sign up to request clarification or add additional context in comments.

Comments

0

The above code doesn't produce multidimensional array but instead one dimensional array like so :

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
    )

)

1 Comment

Joseph Assem Sobhy: this is not an answer. And nobody will receive a notification when you do this. Add a comment to the corresponding answer instead. And you should add an example how you want your array (instead of an excel screenshot).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.