user4943236 user4943236 - 6 months ago 20
PHP Question

PHP: Associative arrays with boolean values

I have an associative php array in which values are boolean. I'm trying to iterate the array and assign it to variables, however, the extracted values comes out blank

var_dump(info) output is:

array(6)
{
["are_all_daily_budgets_spent"]=> bool(false)
["has_account_hit_spend_limit"]=> bool(false)
["has_campaign_group_hit_spend_limit"]=> bool(false)
["is_adgroup_partially_rejected"]=> bool(false)
["is_account_closed"]=> bool(false)
["is_daily_budget_spent"]=> bool(false)
}


I need to extract the boolean values and assign it to separate variables.

I tried doing the following code:

foreach ($info as $key => $value) {
echo $key;
echo $value;
}


But statement echo $value doesn't produce any results.

Also, can you help me producing the following output:
are_all_daily_budgets_spent = 0
has_account_hit_spend_limit = 0

Answer

you can change boolean type to int before echo, check this code:

foreach ($info as $key => $value) {
        echo $key;
        echo (int)$value;    
}

edited (example 2):

foreach ($info as $key => $value) {
        echo '"' . $key . '" = ';
        echo ($value ? 'True/' : 'False/') . (int)$value . "\n";   
}