dongzhuo6137 2012-11-28 08:07
浏览 59
已采纳

php用数组解析多维stdclass对象

I am accessing a wsdl webservice with php. Reading out works great but parsing the result seems kinda difficult: I tried to var_dump() the result to get an idea how the result looks like. This is my PHP code I use to vardump the result:

 foreach($result as $var => $value) {

  var_dump($value);
    echo "<br />";

}

This is what i get with it:

object(stdClass)#3 (3) {
 ["Successful"]=> bool(true) 
 ["MessageId"]=> string(0) "" 
 ["MlsMessageText"]=> string(0) "" 
 } 

object(stdClass)#4 (3) { 

["RowCount"]=> int(3) 
["ColumnCount"]=> int(3) 
["Columns"]=> object(stdClass)#5 (1) { 

    ["Column"]=> array(3) { 

            [0]=> object(stdClass)#6 (2) { 

                ["Name"]=> string(5) "RowID" 
                ["Rows"]=> object(stdClass)#7 (1) { 

                    ["string"]=> array(3) { 
                    [0]=> string(5) "12001" 
                    [1]=> string(5) "12002" 
                    [2]=> string(5) "12003" } } } 

            [1]=> object(stdClass)#8 (2) { 

                ["Name"]=> string(8) "PersonID" 
                ["Rows"]=> object(stdClass)#9 (1) { 

                    ["string"]=> array(3) { 
                    [0]=> string(11) "12033310001" 
                    [1]=> string(11) "12033310002" 
                    [2]=> string(11) "12033310003" } } } 

            [2]=> object(stdClass)#10 (2) { 

                ["Name"]=> string(10) "PersonName" 
                ["Rows"]=> object(stdClass)#11 (1) { 

                    ["string"]=> array(3) { 
                    [0]=> string(10) "Jack Jones" 
                    [1]=> string(11) "Jenifer Row" 
                    [2]=> string(12) "Marin Banker" } } }

        } 
    } 
} 

Its a rather complex answer and I have no Idea how to parse it with looping. Finally i want to get a table containing the data.

Any help very apreciated :)

  • 写回答

3条回答 默认 最新

  • dongsui5464 2012-11-28 08:29
    关注

    The problem is that they transposed the table into column -> row instead of row -> column, you can do that by creating a new table and reverse the columns and rows.

    $table = array();
    
    foreach ($result['xxx']->Columns->Column as $colnr => $coldata) {
        foreach ($coldata->Rows->string as $rownr => $rowdata) {
            $table[$rownr][$coldata->Name] = $rowdata;
        }
    }
    
    print_r($table);
    

    Btw, $result['xxx'] won't work, the 'xxx' should be replaced with the correct key.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题