具有混合stdClass对象和数组的Foreach循环

时间:2011-11-21 15:23:00

标签: php object foreach stdclass

这里我有一个使用Soap的网站输出

stdClass Object 
( 
  [page] => 0 
  [items] => 3 
  [total] => 3 
  [saleItems] => stdClass Object 
  (
    [saleItem] => Array 
    (
      [0] => stdClass Object 
      ( 
        [reviewState] => open 
        [trackingDate] => 2011-11-03T01:06:43.547+01:00 
        [modifiedDate] => 2011-11-03T01:06:43.677+01:00 
        [clickDate] => 2011-10-30T22:57:57.383+01:00 
        [adspace] => stdClass Object 
        (
          [_] => Beslist.nl [id] => 1437603
        ) 
        [admedium] => stdClass Object 
        (
          [_] => 001. Program logo
          [id] => 535098
        ) 
        [program] => stdClass Object 
        (
          [_] => Zavvi NL
          [id] => 8991
        ) 
        [clickId] => 1565847253976339456 
        [clickInId] => 0 
        [amount] => 40.45 
        [commission] => 2.83 
        [currency] => EUR 
        [gpps] => stdClass Object 
        (
          [gpp] => Array 
          (
            [0] => stdClass Object 
            (
              [_] => shoplink 
              [id] => zpar0 
            ) 
          ) 
        ) 
        [trackingCategory] => stdClass Object 
        (
          [_] => Default
          [id] => 45181
        ) 
        [id] => 46a4f84a-ba9a-45b3-af86-da5f3ec29648 
      )
    )
  )
)

我想从程序,佣金和gpp-> _获得数据(带有foreach循环)。我可以从程序和佣金中获取数据:

foreach ($sales->saleItems->saleItem as $sale) {
  $programma    = $sale->program->_;
  $commissie    = $sale->commission;
}

像魅力一样工作。但是我无法从gpp->_获取数据(希望结果是shoplink)。我目前有:

foreach ($sales->saleItems->saleItem->gpps->gpp as $tracking) {
  echo $tracking->_;
}

我收到错误“试图获取非对象的属性”。我已经尝试了很多,如果变化,无法让它工作。我想我真的很亲密。有人有解决方案吗?

1 个答案:

答案 0 :(得分:1)

这应该有效

   foreach ($sales->saleItems->saleItem as $sale) {
        foreach($sale->gpps->gpp as $tracking) {
            echo $tracking->_;
    }

由于saleItem是一个数组,您将无法使用链接。