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) {
  object(stdClass)#7 (3) {
    string(3) "dub"
    string(19) "2019.07.04 09:06:19"
    object(stdClass)#8 (1) {
      object(stdClass)#9 (2) {
        string(11) "From"
        string(10) "1562223979"
  object(stdClass)#10 (1) {
    array(10) {
      object(stdClass)#11 (11) {
        string(13) "4029759107323"
        string(9) "DVD"
        string(58) "Hellraisers"
        string(36) "Freigegeben ohne Altersbeschränkung"
        string(5) "Sport"
        string(4) "2015"
        string(3) "275"
        string(7) "Deutsch"
        string(1) "2"
        string(10) "2049-12-31"
        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".

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



  • ¥50 hyper默认的default switch
  • ¥15 网站打不开,提示502 Bad Gateway
  • ¥20 基于MATLAB的绝热压缩空气储能系统代码咨询
  • ¥15 R语言建立随机森林模型出现的问题
  • ¥20 unity内置语言切换的按钮设置
  • ¥15 中级微观经济学,生产可能性边界问题
  • ¥15 TCP传输时不同网卡传输用时差异过大
  • ¥15 请各位看看我写的属于什么算法,或者有更正确的写法?
  • ¥15 html5 qrcode 扫描器
  • ¥15 爬取网页信息并保存需要完整代码