我有以下代码,$ totalDonations应输出1500。
<?php
$donations = $wpdb->get_results("
SELECT amount
FROM wp_fullstripe_payments
");
?>
<?php $totalDonations = array_sum($donations); ?>
<?php echo $totalDonations; ?>
我从$ totalDonations得到0。
以下是$ donations的var_dump:
array(2) { [0]=> object(stdClass)#2108 (1) { ["amount"]=> string(3) "500" } [1]=> object(stdClass)#2107 (1) { ["amount"]=> string(4) "1000" } }
这也不起作用:
<?php echo $donations[0]; ?>
答案 0 :(得分:1)
您将获得结果作为stdClass
尝试
<?php
$totalDonations;
foreach ($donation as $donations){
totalDonations += $donation->amount;
}
?>
点击此链接:http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/