How to get an object inside another object in php?

0

I have an object inside another object. How do I get this second object? The following image:

$scope.calcular=function(valor){valor.receitaMediaMensal=$scope.receitaMediaMensal;valor.idempresa=$rootScope.idempresa;valor.valorTotalCustoIndireto=$scope.ValorTotalCustoIndireto;valor.valorTotalCustoDireto=$scope.ValorTotalCustoDireto;valor.valorTotalDespesasVariaveis=$scope.ValorTotalDespesasVariaveis;valor.custoIndireto=$scope.custoIndireto;console.log(valor);$http.post(url_mcp,valor).success(function(data){console.log(data);})};

php:

<?phpini_set('display_errors',true);error_reporting(E_ALL);include_once("../con.php");

$pdo = conectar();

$data = file_get_contents("php://input");
$data = json_decode($data);

print_r($data);

$valorPrecoVenda = $data->valorPrecoVenda;
$receitaMediaMensal = $data->receitaMediaMensal;
@$descontoPromo = $data->descPromo;
@$descontoFinan = $data->descFinan;
$valorTotalCustoDireto = $data->valorTotalCustoDireto;
?>
    
asked by anonymous 02.04.2018 / 22:31

1 answer

1

In your example, you have an array containing 2 objects within your costIndirect object (0 and 1).

To access the value of each of them is simple:

$valor_do_0 = $data->custoIndireto[0]->valor;
$valor_do_1 = $data->custoIndireto[1]->valor;

EDIT for dynamic model:

$array_valores = array();
$aux = true;
$cont = 0;

while($aux){
    if(isset($data->custoIndireto[$cont])){
        $array_valores[] = $data->custoIndireto[$cont];
    }else{
        $aux = false;
    }

    $cont++;
}

In this way, it will check if there is the costIndirect at the next position and it will get the value and fill the array $array_valores , if there is no costIndirect at the next position, it will exit the loop.

    
02.04.2018 / 23:05