Search code examples
phparrayskeyvar-dump

Getting empty value from array


I have the following code and $totalDonations should output 1500.

<?php
    $donations = $wpdb->get_results("
      SELECT amount
      FROM wp_fullstripe_payments
    ");
?>

<?php $totalDonations = array_sum($donations); ?>

<?php echo $totalDonations; ?>

I get 0 from $totalDonations.

Here is the var_dump for $donations:

array(2) { [0]=> object(stdClass)#2108 (1) { ["amount"]=> string(3) "500" } [1]=> object(stdClass)#2107 (1) { ["amount"]=> string(4) "1000" } }

This doesn't work either:

<?php echo $donations[0]; ?>

Solution

  • You are getting the result as an stdClass

    Try

    <?php 
         $totalDonations;
         foreach ($donation as $donations){
           totalDonations += $donation->amount;
         } 
    ?>
    

    Check out this link : http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/