我在PHP中有以下代码:
$stmt2 = $dbh->prepare("select GROUP_CONCAT( cohort_id SEPARATOR ',') as cohort_id from ( select distinct cohort_id from table_cohorts) as m");
$stmt2->execute();
$row2 = $stmt2->fetch();
$cohorts_allowed = explode(",",$row2["cohort_id"]);
$value = array ('amount', $cohorts_allowed );
$cohorts_allowed
给了我类似于" database_percent,national_percent"的内容。它是从我的数据库中所有可能的cohort_id
生成的。
我需要做的是获取所有这些并添加额外的价值'金额' (不在我的数据库中)进入数组。
我该怎么做?您可以看到我尝试在上面的代码的最后一行执行此操作,但显然这不起作用。
答案 0 :(得分:1)
$cohorts_allowed = explode(",",$row2["cohort_id"]);
$cohorts_allowed['amount'] = 'amount' ;
或
$cohorts_allowed = explode(",",$row2["cohort_id"]);
$cohorts_allowed[] = 'amount' ;
这是如何运作的:
<pre>
<?php
$row2["cohort_id"] = "database_percent, national_percent";
$cohorts_allowed = explode(",",$row2["cohort_id"]);
print_r($cohorts_allowed);
/* output
Array
(
[0] => database_percent
[1] => national_percent
)
* The last index is 1
*/
$cohorts_allowed[] = 'amount' ;
print_r($cohorts_allowed);
/* output
Array
(
[0] => database_percent
[1] => national_percent
[2] => amount
)
* The last index is 2 (after 1) and have the value amount.
*
*/
?>
</pre>
您可以阅读: