如何在PHP中读取此结果?

时间:2019-04-02 17:51:21

标签: php

如何使用PHP读取此结果? 这是请求结果的print_r:

object(PhpSigep\Services\Result)[687]
  protected 'isSoapFault' => boolean false
  protected 'errorCode' => null
  protected 'errorMsg' => null
  protected 'result' => 
    array (size=38)
      0 => 
        object(PhpSigep\Model\CalcPrecoPrazoResposta)[730]
          protected 'servico' => 
            object(PhpSigep\Model\ServicoDePostagem)[731]
              ...
          protected 'valor' => float 12.28
          protected 'prazoEntrega' => int 5
          protected 'valorMaoPropria' => float 0
          protected 'valorAvisoRecebimento' => float 0
          protected 'valorValorDeclarado' => float 0
          protected 'entregaDomiciliar' => boolean true
          protected 'entregaSabado' => boolean false
          protected 'erroCodigo' => int 0
          protected 'erroMsg' => null
          protected '_failIfAtributeNotExiste' => boolean true
      1 => 
        object(PhpSigep\Model\CalcPrecoPrazoResposta)[732]
          protected 'servico' => 
            object(PhpSigep\Model\ServicoDePostagem)[733]
              ...
          protected 'valor' => float 22.9
          protected 'prazoEntrega' => int 5
          protected 'valorMaoPropria' => float 0
          protected 'valorAvisoRecebimento' => float 0
          protected 'valorValorDeclarado' => float 0
          protected 'entregaDomiciliar' => boolean true
          protected 'entregaSabado' => boolean false
          protected 'erroCodigo' => int 0
          protected 'erroMsg' => null
          protected '_failIfAtributeNotExiste' => boolean true

我想读取数组,我正在尝试$ result-> result,$ result ['result']和其他组合,但是我确实没有设法读取它。 有人可以帮我吗?

1 个答案:

答案 0 :(得分:0)

  • 首先,验证类定义上是否有诸如 getResult()之类的公共方法。

  • 如果您不能修改类定义,请像这样使用Reflection:

代码示例:

function accessProtected($obj, $prop) {
  $reflection = new ReflectionClass($obj);
  $property = $reflection->getProperty($prop);
  $property->setAccessible(true);
  return $property->getValue($obj);
}

调用方法:

$data = accessProtected($result,"result");

要访问数据实例,请递归调用该函数。

如果您想获得 valor prazoEntrega

foreach($data as $CalcPrecoPrazoResposta){
    $valor = accessProtected($CalcPrecoPrazoResposta,"valor");
    $prazoEntrega = accessProtected($CalcPrecoPrazoResposta,"prazoEntrega");
  echo $valor;
  echo $prazoEntrega;
}