【发布时间】:2019-08-09 14:23:23
【问题描述】:
我有这个“优惠券”列表,每个都有一个唯一的“productid”
现在我正在尝试使用以下方法将列表转换为数组:
$claimed = array($rowOrder['productid']);
我的问题是当我尝试使用“count”和“array_sum”时,它会输出单个数字:
$count_claimed = array(count($claimed));
echo array_sum($count_claimed);
使用我得到的回声和输出:“1111111” 我应该改变什么以获得7的总和? (与“优惠券”数量一起显示)
附加信息:
SELECT 语句正在输出“优惠券”,$rowOrder 正在调用它。
public function SelectLst_ByUsrCustomerIDInfo($db, $usrcustomerid) {
$stmt = $db->prepare(
" SELECT o.orderid, o.productid, o.usrcustomerid, o.amount, o.amountrefunded, o.createddate, o.scheduleddate, o.useddate, o.expirationdate, p.photosrc
FROM `order` o LEFT JOIN `product` p ON o.productid = p.productid
WHERE usrcustomerid = :usrcustomerid"
);
$stmt->bindValue(':usrcustomerid', $usrcustomerid, PDO::PARAM_INT);
$stmt->execute();
$rows = $stmt->fetchAll(PDO::FETCH_ASSOC);
return $rows;
}
这样称呼
$lstInfo = $mcOrder->SelectLst_ByUsrCustomerIDInfo($db, $usrcustomerid);
foreach($lstInfo as $rowOrder) {
if (isset($rowOrder['productid']) && ($rowOrder['expirationdate'] > date("Y-m-d H:i:s"))) {
$claimed = array($rowOrder['productid']);
$count_claimed = array(count($claimed));
echo array_sum($count_claimed);
}
}
【问题讨论】:
-
$claimed = array($rowOrder['productid']);也将是一个长度为 one 的数组。您可能希望将其推送到数组而不是将其分配给数组,然后单独使用count()(array_sum()将对包含许多数字的数组的所有值求和)。 -
您应该显示更多代码 - 您是否从查询中获取此数据?如果是这样,你能分享那个代码吗?
-
用附加信息更新了我的问题
-
你怎么称呼
SelectLst_ByUsrCustomerIDInfo?也显示该代码。 -
再次更新问题