dongzaotiao2863 2011-12-15 12:22
浏览 30
已采纳

试图访问数组内的对象

As a service result I have this array:

array(1) {
  [0]=>
  object(stdClass)#322 (2) {
    ["NOME_PROPRIEDADE"]=>
    string(8) "MATERIAL"
    ["VALORES"]=>
    object(stdClass)#323 (1) {
      ["ValoresPossiveisVO"]=>
      array(5) {
        [0]=>
        object(stdClass)#324 (1) {
          ["VALOR"]=>
          string(15) "CRISTAL INCOLOR"
        }
        [1]=>
        object(stdClass)#325 (1) {
          ["VALOR"]=>
          string(21) "CRISTAL FOTOCROMATICO"
        }
        [2]=>
        object(stdClass)#326 (1) {
          ["VALOR"]=>
          string(6) "RESINA"
        }
        [3]=>
        object(stdClass)#327 (1) {
          ["VALOR"]=>
          string(13) "POLICARBONATO"
        }
        [4]=>
        object(stdClass)#328 (1) {
          ["VALOR"]=>
          string(6) "TRIVEX"
        }
      }
    }
  }

I'm accessing its values like this:

foreach ($res->ObterValoresDosTiposDeProdutoParaWebSiteResult->PropriedadesPossiveisVO as $material)
{
echo $material->NOME_PROPRIEDADE; //it returns me MATERIAL, as expected
}

It works but when I try to access $material->VALORES is returns me that it is a empty array: array(o){}

What am I doing wrong?

Thanks in advance for any help.

  • 写回答

1条回答 默认 最新

  • duanji1924 2011-12-15 12:26
    关注

    Because the outermost item ($materail) is an array, its internals need to be accessed by [] index. You need $material[0]->VALORES, which is then an object containing objects and arrays. So to get its internal items,

    $material[0]->VALORES->ValoresPossiveisVO[0]->VALOR;
    // "CRISTAL INCOLOR"
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?