Value in particular within an array

1

I have a dilemma when printing a variable, you see, this is my query in SQL:

SELECT MONTH(fecha_del_deposito) AS mes, SUM(importe) as monto FROM pago_cliente

And it throws the following:

Which happens to query in php:

'cantidadPagos'  => $this->Backend_model->rowCountPagos("pago_cliente"),



public function rowCountPagos($tabla)
{
   $this->db->select("MONTH(fecha_del_deposito) as mes, SUM(importe) as monto");
    $this->db->from($tabla);
    $resultados = $this->db->get();
    return $resultados->result();
}

If I print it with print_r it results in this:

Array ( [0] => stdClass Object ( [mes] => 3 [monto] => 3484 ) )

And I need to print only the value of "Amount" (in this case 3484) but I do not find the appropriate form, what I do so far is:

<?php echo $cantidadPagos[1]; ?>

I know it's completely wrong, but I do not know how to do it, I need help.

Thanks

    
asked by González 27.03.2018 в 20:04
source

1 answer

3

How could you observe when doing print_r to your variable Array ([0] => stdClass Object ([month] = > 3 [amount] => 3484)), this contains is an array of 1 single element and this is an object of type stdClass, as you did not indicate what the name was of the variable where you have assigned the result of your query I will assume that it is called $ result

$result = [
    'cantidadPagos'  => $this->Backend_model->rowCountPagos("pago_cliente"),
]

Now if you want to print only the amount:

$pagos = $result['cantidadPagos'];
echo $pagos[0]->monto;

//O si no te enredas con los []
echo $result['cantidadPagos'][0]->monto;

The stdClass objects are actually the way to represent an array as an instance of a class , to access your keys you must use the arrow operator - > they become properties

    
answered by 27.03.2018 / 20:25
source