doupa8922 2019-05-24 07:39 采纳率: 100%
浏览 78

访问多维stdClassObject - 数组

I want to access the array properties I get as a SOAP-Resonse from a server. I use the php soapclient and and get the following output when using

$response = $client->$action($clientID);
$array = json_decode(json_encode($xml), True);

Unfortunately I cannot get access to the properties such as 'tid', 'answer' etc. How can I do this (I can either use php or c# where I import the result)

Array
(
    [0] => Array
        (
        )

    [1] => Array
        (
            [0] => stdClass Object
                (
                    [tid] => 4103
                    [tdid] => 191
                    [qid] => 4103-1
                    [question] => Wie würden Sie Ihren Gesundheitszustand im Allgemeinen beschreiben ?
                    [answer] => Ausgezeichnet.
                    [score] => 100
                    [date] => 1558593404
                    [Fields] => Array
                        (
                        )

                )

            [1] => stdClass Object
                (
                    [tid] => 4103
                    [tdid] => 191
                    [qid] => 4103-2
                    [question] => Im Vergleich zum vergangenen Jahr, wie würden Sie Ihren derzeitigen Gesundheitszustand beschreiben ?
                    [answer] => Derzeit etwas besser als vor einem Jahr.
                    [score] => 75
                    [date] => 1558593404
                    [Fields] => Array
                        (
                        )

                )                                   
  • 写回答

2条回答 默认 最新

  • doudun1934 2019-05-24 07:53
    关注

    By looking at the result, can you try this?

    // assuming $array is the array that you described
    
    $tid = ($array[0])->tid;
    

    because $array is an array of Objects, so i assume to get the data is as simple as using the property accessor ->. so $tid

    you can learn more about php objects here

    hope it helps!

    -- EDIT --

    it should be ($array[1][0])->tid. Because $array[0] is empty. you can use PHP's array_filter to remove all the empty arrays in $array.

    $filtered_array = array_filter($array, function($el){
      return count($el) > 0;
    });
    
    

    $filtered_array should only have non-empty arrays in it. but do take note that array_filter preserves array keys.

    then, you can get all the objects with foreach loops.

    // say you want to put all tids in an array
    
    $array_of_tids = [];
    foreach($filtered_array as $array_of_objects){
      foreach($array_of_objects as $response_objects){
        $array_of_tids[] = $response_objects->tid;
      }
    }
    

    the simpler but more unreliable (since i don't know the API response's consistency), would be to just use the index.

    $tid_of_first_object = ($array[1][0])->tid
    // $tid_of_first_object would be '4103'
    

    let me know if it's still not working!

    评论

报告相同问题?

悬赏问题

  • ¥15 请问我该如何添加自己的数据去运行蚁群算法代码
  • ¥20 用HslCommunication 连接欧姆龙 plc有时会连接失败。报异常为“未知错误”
  • ¥15 网络设备配置与管理这个该怎么弄
  • ¥20 机器学习能否像多层线性模型一样处理嵌套数据
  • ¥20 西门子S7-Graph,S7-300,梯形图
  • ¥50 用易语言http 访问不了网页
  • ¥50 safari浏览器fetch提交数据后数据丢失问题
  • ¥15 matlab不知道怎么改,求解答!!
  • ¥15 永磁直线电机的电流环pi调不出来
  • ¥15 用stata实现聚类的代码