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

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

报告相同问题?

悬赏问题

  • ¥20 创建taro项目,在vscode上能够写跨平台写微信小程序代码
  • ¥15 网站出现错误跳转问题
  • ¥15 Docker容器里已经安装了ssh,但打包迁移到新机器一直容器一直提示unrecognized service。
  • ¥15 综合布线实例设计,就好看好看不恐怖可好滤镜好聚
  • ¥15 使用moviepy库视频合并时出错
  • ¥30 FLUENT液固传质UDF
  • ¥15 怎么看梯度直方图以,怎么判断梯度消失/爆炸,怎么解决
  • ¥15 aspnetdll文件访问拒绝
  • ¥15 wpf中在模版中寻找元素
  • ¥15 MFC平台生成指定圆