【问题标题】:php parsing multidimensional stdclass object with arraysphp用数组解析多维stdclass对象
【发布时间】:2012-11-16 00:51:56
【问题描述】:

我正在使用 php 访问 wsdl 网络服务。读出效果很好,但解析结果似乎有点困难:我试图 var_dump() 结果来了解结果的样子。这是我用来转储结果的 PHP 代码:

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

        } 
    } 
} 

这是一个相当复杂的答案,我不知道如何通过循环解析它。最后我想得到一个包含数据的表。

非常感谢任何帮助:)

【问题讨论】:

    标签: php arrays web-services parsing stdclass


    【解决方案1】:

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

    【讨论】:

    • 但是如果我这样循环,我无法检测出我给出的 $value 是列的 ["Name"] 还是 ['value' ],对吗? (对不起,我还是 php 的初学者...)
    【解决方案2】:

    问题是他们将表格转换为column -&gt; row 而不是row -&gt; 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' 应替换为正确的密钥。

    【讨论】:

    • 我更喜欢这种方式。一个非常好的解决方案,并且使得之后处理数据变得非常容易。非常感谢!
    【解决方案3】:

    我有类似的问题。我从 JSON 形式的代码中获取结果。当我使用 json_decode 时,我注意到通过 var_dump 方法在解码过程中在数组中创建了一些对象。

    你在下面看到的 var_dump 是我的 var

    $result.
    

    如果我想从我的 var 访问“status”数组,我使用了以下方法,效果很好:)

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

    【讨论】:

      猜你喜欢
      • 2016-03-18
      • 1970-01-01
      • 2013-10-12
      • 2014-02-28
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-04-29
      相关资源
      最近更新 更多