1

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

Siong Thye Goh
  • 3,518
  • 10
  • 23
  • 31
  • 1
    By `Titel` you mean `Title`? As you desire output for that example will be "Hellraisers"? – dWinder Jul 04 '19 at 07:45
  • 1
    As the data looks as though it is under another element - try `foreach ($results['Products'] as $result) {` in your last effort. (along with the correct spelling). – Nigel Ren Jul 04 '19 at 07:46
  • The 2 objects in the resulting array seem very different. I don't think a loop would be the best way to get these results. – Dirk Scholten Jul 04 '19 at 07:46
  • Thanks for the quick responses! @Nigel When i try this, i get Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /opt/lampp/htdocs/api_test/index.php:39 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/api_test/index.php on line 39 – youwhatmate Jul 04 '19 at 07:51
  • @Dirk What other way can i try? – youwhatmate Jul 04 '19 at 07:51
  • can you print whole response that you have got from API? – Bhavin Thummar Jul 04 '19 at 07:54
  • That because `$results['Products']` is object with attribute "Product" which doesn't make sense as it should be array... – dWinder Jul 04 '19 at 07:54
  • The whole print is too long for the comment section – youwhatmate Jul 04 '19 at 08:00
  • Notice: Undefined index: Product in /opt/lampp/htdocs/api_test/index.php on line 40 Notice: Trying to get property 'Title' of non-object in /opt/lampp/htdocs/api_test/index.php on line 40 Titel – youwhatmate Jul 04 '19 at 08:00

1 Answers1

0

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

Community
  • 1
  • 1
Josep Vidal
  • 2,580
  • 2
  • 16
  • 27
  • Hey Josep! Thank you very much for the good process explanation.It helps me understand it a little bit better. When I try your approach, I get this error message: Fatal error: Uncaught Error: Cannot use object of type stdClass as array in /opt/lampp/htdocs/api_test/index.php:39 Stack trace: #0 {main} thrown in /opt/lampp/htdocs/api_test/index.php on line 39 If try the Products as an object ($response->Products['Product'] i get the same message I just get data from the api, i didnt write it myself. This is the JSON I get from it – youwhatmate Jul 04 '19 at 08:38
  • @youwhatmate try `echo $product->Title`, instead of echo $product['Title']; . If it is not working use the next code `foreach($response["Products"]->Product as $product){ echo $product->Title; // prints the title of every product }` – Josep Vidal Jul 04 '19 at 08:46
  • Ok after some Trial&Error I got my result :-) foreach($response->Products->Product as $product) { echo $product->Title . "
    "; }
    – youwhatmate Jul 04 '19 at 08:51
  • @youwhatmate as you did not put the entire code I did not know that the main array was also an object, I'll edit the code, mark as right then, thanks! – Josep Vidal Jul 04 '19 at 08:52
  • Sorry for that! I marked your answer as the right one! Thank you! – youwhatmate Jul 04 '19 at 08:55