php parsing multidimensional stdclass object with arrays

扶醉桌前 提交于 2020-01-04 04:11:06

问题


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 :)


回答1:


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:


Objects are itteratable. So you can loop through them with foreach. Just create recursive function with foreach loop in it. Like that for example:

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



回答3:


I had kind of similar question. I was getting results back from a code in the form of JSON. When I used json_decode I noticed via var_dump method that during decoding some objects were created inside the array.

The var_dump you see below is for my var

$result.

If I want to access "status" array from my var I used the following which worked just fine :)

$result[0]->status

I wanted the data in that which had my query results.

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) "tkamran@premierbpo.com"
        ["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"
      }
    }
  }
}


来源:https://stackoverflow.com/questions/13600531/php-parsing-multidimensional-stdclass-object-with-arrays

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!