1

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

6
  • 5
    echo $value ? 'true' : 'false'; Commented May 12, 2016 at 7:02
  • 2
    Possible duplicate of PHP - Get bool to echo false when false Commented May 12, 2016 at 7:03
  • Question is, why do you want that output? Is this for debugging only? Then var_dump is a better option. If the value is needed somewhere else in your script, you'd be well adviced to not change it to some string representation. Commented May 12, 2016 at 7:09
  • I need to extract the key value pairs in different variables and then further need to put some conditions on these results Commented May 12, 2016 at 7:11
  • Then keep the values as is, php knows how to handle true and false without it being human-readable, no need to change it. Commented May 12, 2016 at 7:11

4 Answers 4

4

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.

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

Comments

1

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

3 Comments

would you mind sharing how do I assign the individual boolean value to variable. I need output such as "are_all_daily_budgets_spent" = False/0
basically, I'm looking to get the output having variable names same as the key names but holding the values. example variable are_all_daily_budgets_spent should have False value when echo(are_all_daily_budgets_spent)
are_all_daily_budgets_spent should have False value (boolean type) or 'False' string type value?
1

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.

Comments

0

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/>';
}

Comments

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.