1

我在这里的处境非常艰难。

这些对象有一个数组

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
)

我正在尝试将其转换为类似于此结构的多维数组

$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'
                )
        )

有什么我想念的吗,这真的很容易做到吗?请给我建议。我尝试遍历这些对象,但它仍然不起作用并且看起来很丑。

4

1 回答 1

1

您应该使用该功能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;
}
于 2012-04-04T16:53:13.080 回答