1

I have an one doubt with conditional array in php.

Here is my code,

foreach($orders as $order) {
array_push($list, array($order['name'],$order['email'],$order['buyer_accepts_marketing']));
}

Still now i got the value of $order['buyer_accepts_marketing'] is 1, but I want value as true.

I tried, but didn't get exact answer.

Can anyone help me? Thanks in advance!.

2 Answers 2

2

You can cast your value as boolean (c.f. http://www.php.net/manual/en/language.types.type-juggling.php):

foreach($orders as $order) {
    array_push($list, array(
        $order['name'],
        $order['email'],
        (boolean) $order['buyer_accepts_marketing']
    ));
}

Just FYI, a shortcut for array_push would be:

foreach($orders as $order) {
    $list[] = array(
        $order['name'],
        $order['email'],
        (boolean) $order['buyer_accepts_marketing']
    );
}

I generally find that it looks nicer :)

If you want to do more complex logic to get your boolean value you could create a new variable for it:

foreach($orders as $order) {
    $buyer_accepts_marketing = false;
    if( $order['buyer_accepts_marketing'] ) $buyer_accepts_marketing = true;

    array_push($list, array(
        $order['name'],
        $order['email'],
        $buyer_accepts_marketing
    ));
}

If you want to instead set your buyer_accepts_marketing value to a string value of "true" or "false", then do:

foreach($orders as $order) {
    $buyer_accepts_marketing = "false";
    if( $order['buyer_accepts_marketing'] ) $buyer_accepts_marketing = "true";

    array_push($list, array(
        $order['name'],
        $order['email'],
        $buyer_accepts_marketing
    ));
}

Just be aware that a string "true" is not boolean, nor is "false". For example:

if( "true" == true ) echo "It's true";

Will echo "It's true". However, so will:

if( "false" == true ) echo "It's true";

That's because any string that isn't set to "0" or "" will evaluate as true.

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

24 Comments

I also had similar problem some time back. I checked with same code as you mentioned above but this code still gives value as "1" when array is printed. I tried both of your codes.
@Purushottamzende How are you printing the array?
Using "print_r". I also tried by saving them in DB and also by displaying it in string, but no success.
@Purushottamzende use var_dump to print this array
@Purushottamzende print_r doesn't seem to explicitly acknowledge booleans, but it it is in fact a boolean. See this example: codepad.org/5fzAbKO8
|
1

You can simply use

foreach($orders as $order) {
if( $order['buyer_accepts_marketing'] ){ 
       $buyer_accepts_marketing = "true";
}
$list[] = array(
        $order['name'],
        $order['email'],
        $buyer_accepts_marketing; 
    );
}

4 Comments

Yes, I got it. Thank you so much.
@ railsbox: I have an one doubt, in your answer, $order['buyer_accepts_marketing'] value is whatever it is, it will display as true right?..
@user3667492 if $order['buyer_accepts_marketing'] value will be 0 aur empty (null) it will not enter in condition
Thanks again!:). but if it is 0 , I will get false.. Can you help me?.

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.