user4943236
user4943236

Reputation: 6334

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

Upvotes: 1

Views: 8673

Answers (4)

axiac
axiac

Reputation: 72226

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

Indeed, in PHP, the boolean value true is displayed as 1 and the boolean value false is displayed as the empty string (i.e. nothing is displayed). One of the reasons of this decision could be the need to be consistent with the way PHP converts values of other types to boolean.

For debugging purposes you can use var_dump() instead of echo(). It shows you both the value of the variable in an unambiguous way and its type:

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

But you already know this, the text representation of the array you posted in the question was generated using var_dump($info);.


Update

If you need to generate variables from the keys and values of the array you have many options.

  1. You can extract the values one by one (not necessarily all of them) and create variables in the local scope:

    $are_all_daily_budgets_spent = $info['are_all_daily_budgets_spent'];
    $has_account_hit_spend_limit = $info['has_account_hit_spend_limit'];
    // ...
    

    If you want to have integer values instead (1 for TRUE, 0 for FALSE) you just convert the values extracted from array to integer:

    $are_all_daily_budgets_spent = (int)$info['are_all_daily_budgets_spent'];
    $has_account_hit_spend_limit = (int)$info['has_account_hit_spend_limit'];
    // ...
    
  2. You can do the same as above but in a loop (and extract all the values contained in the array):

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

    Here, $$key uses the feature called variable variables. It first interprets $key, gets its value (the string are_all_daily_budgets_spent) then it interprets $are_all_daily_budgets_spent as a new variable, creates it and stores (int)$value in it. Basically, it's the same we did on previous method, just automated.

  3. You can also use the PHP function extract():

    extract($info);
    

    It basically produces the same outcome as the code on item #2.

Warning

Do not use methods #2 and #3 when the data stored in $info comes from external sources (browser, APIs, user input etc). It poses a high security risk because it can overwrite your variables with values you don't control.

Upvotes: 4

ssnake
ssnake

Reputation: 375

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";   
}

Upvotes: 1

Majid Abbasi
Majid Abbasi

Reputation: 1531

In php you can not echo a variable which has boolean value, in this cases you can check that variable and convert that to an string. you can see this process in the following example:

foreach ($info as $key => $value) 
{
     $output = $value ? 'true' : 'false';
     echo $key.'='.$output.'<hr/>';
}

Upvotes: 0

Rosh Donniet
Rosh Donniet

Reputation: 418

In PHP, trying to echo a boolean will never actually print "true" or "false".

echo true;

will print 1, and

echo false;

just won't print anything. Try this instead :

echo $boolean ? 'true' : 'false';

This uses the ternary operator.

Upvotes: 1

Related Questions