2012-11-28 52 views
3

我正在訪問與PHP的wsdl webservice。讀出效果很好,但解析結果似乎有點困難:我試圖通過var_dump()結果來了解結果的外觀。這是我的PHP代碼,我用它來vardump結果:php解析數組的多維stdclass對象

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

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

} 

這就是我得到它:

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" } } } 

     } 
    } 
} 

它是一個相當複雜的答案,我不知道如何與循環解析它。最後我想得到一個包含數據的表格。

任何幫助非常apreciated :)

回答

2

的問題是,他們調換表爲column -> row,而不是row -> column,你可以通過創建一個新的表和反向列和行。

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

順便說一句,$result['xxx']將無法​​正常工作時,應'xxx'使用正確的密鑰進行更換。

+0

我更喜歡這種方式。一個非常好的解決方案,並使得事後處理數據變得非常容易。非常感謝你! – colosso

3

Objects are itteratable。所以你可以用foreach循環它們。只需用foreach循環創建遞歸函數即可。這樣的例子:

function loop($input) 
{ 
    foreach ($input as $value) 
    { 
     if (is_array($value) || is_object($value)) 
      loop($value); 
     else 
     { 
      //store data 
      echo $value; 
     } 
    } 
} 
+0

但是,如果我這樣循環,我不能檢測到我給出的$值是列的[「Name」]還是['value'],是嗎? (對不起,我仍然是初學者...) – colosso

0

我曾經有過類似的問題。我從JSON形式的代碼中獲取結果。當我使用json_decode時,我通過var_dump方法注意到在解碼過程中一些對象是在數組內部創建的。

你看到下面的的var_dump是我的VAR

$result. 

如果我要訪問從我的VAR「狀態」陣列我用它工作得很好:)

$result[0]->status 

我以下想要那些有我的查詢結果的數據。

array(1) { 
    [0]=> 
    object(stdClass)#3 (3) { 
    ["status"]=> 
    string(9) "Connected" 
    ["message"]=> 
    string(34) "Connected to database successfully" 
    ["data"]=> 
    array(1) { 
     [0]=> 
     object(stdClass)#2 (10) { 
     ["id"]=> 
     string(36) "cc569871-6544-11e3-945d-0e184c35292b" 
     ["login_name"]=> 
     string(22) "[email protected]" 
     ["login_password"]=> 
     string(8) "Lead6291" 
     ["notes"]=> 
     NULL 
     ["created_by"]=> 
     string(7) "default" 
     ["created_on"]=> 
     string(19) "2013-12-15 12:52:46" 
     ["last_modified_by"]=> 
     string(7) "default" 
     ["last_modified_on"]=> 
     string(19) "2013-12-15 12:52:46" 
     ["is_current"]=> 
     string(3) "yes" 
     ["is_active"]=> 
     string(3) "yes" 
     } 
    } 
    } 
}