2

I have the following code in 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 gives me something like "database_percent, national_percent". It is generated from all the possible cohort_ids in my database.

What I need to do is get all of these and add in the additional value 'amount' (which is not in my database) into the array.

How can I do this. You can see I tried to do this on the last line of my code above, but obviously that doesn't work.

0

1 Answer 1

1
$cohorts_allowed = explode(",",$row2["cohort_id"]);
$cohorts_allowed['amount'] = 'amount' ;

or

$cohorts_allowed = explode(",",$row2["cohort_id"]);
$cohorts_allowed[] = 'amount' ;

How this works:

<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>

You can read in:

http://php.net/manual/en/language.types.array.php

Sign up to request clarification or add additional context in comments.

2 Comments

@jonmrich I edit with a example, don't forget check the answer as correct ;-)
Thanks for the additional insight.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.