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

9
  • 1
    By Titel you mean Title? As you desire output for that example will be "Hellraisers"? Commented Jul 4, 2019 at 7: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). Commented Jul 4, 2019 at 7: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. Commented Jul 4, 2019 at 7: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 Commented Jul 4, 2019 at 7:51
  • @Dirk What other way can i try? Commented Jul 4, 2019 at 7:51

1 Answer 1

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

Sign up to request clarification or add additional context in comments.

5 Comments

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 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 }
Ok after some Trial&Error I got my result :-) foreach($response->Products->Product as $product) { echo $product->Title . "<br>"; }
@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!
Sorry for that! I marked your answer as the right one! Thank you!

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.