dou8mwz5079 2012-04-04 16:46
浏览 91
已采纳

PHP从对象属性创建一个多维数组

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条回答 默认 最新

  • doushenmao9036 2012-04-04 16:53
    关注

    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;
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 R语言卸载之后无法重装,显示电脑存在下载某些较大二进制文件行为,怎么办
  • ¥15 java 的protected权限 ,问题在注释里