迭代通过多维对象/数组

问题描述:

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

我有一些soap api的数据。 此数据采用以下格式: p>

  array(2){
 [“Request”] => 
 object(stdClass)#7(3){
  [“AccessKey”] => 
 string(3)“dub”
 [“Timestamp”] => 
 string(19)“2019.07.04 09:06:19”
 [“条件”]  => 
 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)“FreigegebenohneAltersbeschränkung”
 [“Genre”] => 
 string(5)“Sport”
 [“Year”] =>  
 string(4)“2015”
 [“长度”] => 
 string(3)“275”
  [“Language”] => 
 string(7)“Deutsch”
 [“Items”] => 
 string(1)“2”
 [“Release”] => 
 string(  10)“2049-12-31”
 [“标签”] => 
字符串(17)“Edel Germany GmbH”
} 
  code>  pre> 
 
 

我想遍历这些数据并得到每个集合的标题。 p>

我尝试了一个foreach循环,但是我得到了一些错误消息。 p>

  foreach($ results as $ result){
 echo“Titel”。  $ result->标题; 
} 
 
  code>  pre> 
 
 
  foreach($ results as $ result){
 echo“Titel”。  $ result ['Product']  - >标题; 
} 
 
  code>  pre> 
 
 

没有任何效果。 我无法围绕数组... p> div>

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