dqjjw04440 2019-07-04 07:43 采纳率: 0%
浏览 120
已采纳

迭代通过多维对象/数组

I have some data out of a soap api. This data comes in this format:

array(2) {
  ["Request"]=>
  object(stdClass)#7 (3) {
    ["AccessKey"]=>
    string(3) "dub"
    ["Timestamp"]=>
    string(19) "2019.07.04 09:06:19"
    ["Conditions"]=>
    object(stdClass)#8 (1) {
      ["Condition"]=>
      object(stdClass)#9 (2) {
        ["Field"]=>
        string(11) "From"
        ["Value"]=>
        string(10) "1562223979"
      }
    }
  }
  ["Products"]=>
  object(stdClass)#10 (1) {
    ["Product"]=>
    array(10) {
      [0]=>
      object(stdClass)#11 (11) {
        ["Ean"]=>
        string(13) "4029759107323"
        ["Type"]=>
        string(9) "DVD"
        ["Title"]=>
        string(58) "Hellraisers"
        ["FSK"]=>
        string(36) "Freigegeben ohne Altersbeschränkung"
        ["Genre"]=>
        string(5) "Sport"
        ["Year"]=>
        string(4) "2015"
        ["Length"]=>
        string(3) "275"
        ["Language"]=>
        string(7) "Deutsch"
        ["Items"]=>
        string(1) "2"
        ["Release"]=>
        string(10) "2049-12-31"
        ["Label"]=>
        string(17) "Edel Germany GmbH"
      }

I want to loop through this data and get the title of every set.

I tried a foreach loop, but I get some error messages.

foreach ($results as $result) {
    echo "Titel " . $result->Titel;
}

foreach ($results as $result) {
    echo "Titel " . $result['Product']->Titel;
}

Nothing works. I can't wrap my head around arrays...

  • 写回答

1条回答 默认 最新

  • dongliulu1122 2019-07-04 08:11
    关注

    When you don't grasp something, sometimes its better to try to make it small and grow from there, start trying to print the entire response, then the property products, then product and then the product array:

    How to reach the products array inside $response

    First you have an object $response has elements with objects inside, ["Products"] is the one we want, so $response->Products then inside ["Products"] there is an object, with one property with the name of ["Product"] that contains the array of objects with all the products, so $response>Products->Product. As $response->Products->Product is an array we need to iterate it, you iterate like this:

    foreach($response->Products->Product as $product){
     echo $product->Title; // prints the title of every product
    }
    

    Don't hesitate to ask if you don't understand or it is not working, but by the code you pasted I thing the foreach is correct.

    How to access a property of an object (stdClass Object) member/element of an array? [duplicate]

    JSON format advice

    By the way, the JSON is not "clear" and "correct", the array of products should be one level above. Inside "Products" and not inside "Product".

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题