How to catch an object inside another object in php?

Asked

Viewed 308 times

0

I have an object inside another object. How do I catch this second object? Follow picture: inserir a descrição da imagem aqui

$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:

<?php
ini_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;
?>
  • 1

    If possible post your code. It gets easier. You also used the tag PHP, but posted an image with output of Javascript ?!

  • I’m sending by JS, angled and picking by php. What I sent is how JS is sending to php

  • Code posted @Valdeirpsr

  • Which object do you want to handle? the account?

  • No, value... I just need the value.

  • I’m using a for take-by-position, but I’m not getting: $Qtd = Count($costIndirect); $pos = 0; for($i=0; $i <= $Qtd; $i++){ $pos = $costIndirect[$i]->value; echo $pos." n"; }

  • echo $data->custoIndireto[0]->valor; You can use reset to take the first index of the array or end to catch the last. Or still a for or foreach.

  • Thanks for the @Valdeirpsr tip, but how can I take this data inside a for, because I will have more of a value to take, you understand?

  • @GustavoSevero https://hastebin.com/inugokerec.xml

  • It’s @Valdeirpsr, I tried it with for, but it’s a mistake: "<b>Notice</b>: Undefined offset: 2 in <b>/Applications/MAMP/htdocs/systems/webApps/fluxo_de_box/fluxojoin_2.0/php/pricing/matrizCalculoPrecificacao.php</b> on line <b>45</b><br /> <br /> <b>Notice</b>: Trying to get Property of non-Object in <b>/Applications/MAMP/htdocs/systems/webApps/fluxo_de_box/fluxojoin_2.0/php/pricing/matrizCalculoPrecificacao.php</b> on line 45" This line 45 is: echo $data->costIndirect[$i]->value, PHP_EOL;

  • Another thing, I need to put a different variable those values, so I tried it. With foreach there is no error, but I cannot separate the values into different variables.

  • @Gustavosevero fix the error and modified example #2. I added an example of how to create and capture variables with dynamic names. https://hastebin.com/aqohokayoj.xml

  • All right, thanks...

Show 8 more comments

1 answer

1

In your example, you have an array containing 2 objects within your Direct 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++;
}

This way, it will check if there is the cost Direct at the next position and will take the value and fill the array $array_valores, if there is no cost.

  • Yes David, but I want to take these values dynamically and not putting [0] and [1], you know? I’ve tried with for, but it gives this error: Notice: Undefined offset: 2 in <b>/Applications/MAMP/htdocs/systems/webApps/fluxo_de_box/fluxojoin_2.0/php/pricing/matrizCalculoPrecificacao.php</b> on line 45 Notice: Trying to get Property of non-Object in <b>/Applications/MAMP/htdocs/systems/webApps/fluxo_de_box/fluxojoin_2.0/php/pricing/matrizCalculoPrecificacao.php</b> on line 45"

  • It is possible to do with isset, to avoid the error. I will edit the answer, see if it solves your situation.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.