1

I have a really tough situation in here.

There is an array of these objects

ProductionItem Object
(
    [customer] => Customer Object
        (
            [customer_id] =>24
            [company_type] => Limited Company
            [date_created] => 2009-01-28 15:55:50
            [vat] => 
            [status] => 1
            [account_number] => 590893
        )

    [woi_id] => 67017
    [od_id] => 7154
    [pd_id] => 1112
    [od_qty] => 0
    [od_color] => TEAL BLUE
)

I am trying to convert it to a multidimensional array of something similar to this structure

$allOrders[24] = array (
        objects[0] => array(
                'woid_ud' = 67017,
                'od_id' = 7154,
                'pd_id' = 1112,
                'od_qty' = 0,
                'od_color' = 'TEAL BLUE'
                ),
        objects[1] => array(
                'woid_ud' = 75839,
                'od_id' = 5890,
                'pd_id' = 2344,
                'od_qty' = 0,
                'od_color' = 'TEAL GRAY'
                )
        )

Is there anything I am missing and this is really easy to do? Please, advice me. I tried iterating through those object and it still does not work and looks ugly.

1 Answer 1

1

You should use the function get_object_vars.

function object_to_array($obj){
    if(!is_object($obj)){
       return $obj;
    }
    $data = array();
    foreach(get_object_vars($obj) as $k=>$v){
        $data[$k] = object_to_array($v);
    }
    return $data;
}
Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the reply. But the problem I have is slightly different. I need ProductionItem->customer->customer_id to become the index of the array and [woi_id] => 67017 [od_id] => 7154 [pd_id] => 1112 [od_qty] => 0 [od_color] => TEAL BLUE to be accessible via array[0][43]['od_id']. Then, as it as an array of objects, via array[1][99]['od_id'], where 99 is customer_id
Thank you for response. I figured out how to do the task in a different and more elegant way without a multidimensional array.

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.