PHP JSon How to read boolean value received in JSon format and write in string on PHP

26,599

Solution 1

you can use it like this, in JSON format when you evaluate false value it will give you blank, and when you evaluate true it will give you 1.

$str = '[{"clientId":"17295c59-4373-655a-1141-994aec1779dc","channel":"\/meta\/connect","connectionType":"long-polling","ext":{"fm.ack":false,"fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"},"id":"5"}]';

$arr = json_decode($str,true);

if($arr[0]['ext']['fm.ack'])    // suggested by **mario**
{
    echo "true";    
}
else {
    echo "false";   
}

Solution 2

I know there is already an answer to this but it may be worth noting that var_dump outputs Boolean values better it just has worse formatting IMO.

<pre>
    <?php
        print_r(array(true, false));
        var_dump(array(true, false));
    ?>
</pre>

Results in

Array
(
    [0] => 1
    [1] => 
)
array(2) {
  [0]=>
  bool(true)
  [1]=>
  bool(false)
}
Share:
26,599
LiTHiUM2525
Author by

LiTHiUM2525

I'm working on GayUrban website and ios / android / windows apps. GayUrban is a platform for LGBTI (Lesbian, Gay, Bisexual, Transgender and Intersex) community.

Updated on November 26, 2020

Comments

  • LiTHiUM2525
    LiTHiUM2525 over 3 years

    I receive this JSON string from another site and I cannot modify what received from. The string is receive in $_POST and is :

    [
        {
            "clientId":"17295c59-4373-655a-1141-994aec1779dc",
            "channel":"\/meta\/connect",
            "connectionType":"long-polling",
            "ext":{
                "fm.ack":false,
                "fm.sessionId":"22b0bdcf-4a35-62fc-3764-db4caeece44b"
            },
            "id":"5"
        }
    ]
    

    I decode the JSON string with the following code :

    $receive = json_decode(file_get_contents('php://input'));
    

    And when I use print_r($receive) I get the following:

    Array (
    [0] => stdClass Object
        (
            [clientId] => 17295c59-4373-655a-1141-994aec1779dc
            [channel] => /meta/connect
            [connectionType] => long-polling
            [ext] => stdClass Object
                (
                    [fm.ack] => 
                    [fm.sessionId] => 22b0bdcf-4a35-62fc-3764-db4caeece44b
                )
    
            [id] => 5
        )
    )
    

    I can access and read all Array / Object with no problem :

    $receive[$i]->clientId;
    $receive[$i]->channel;
    $connectionType = $receive[$i]->connectionType;
    $receive[$i]->id;
    $receive[$i]->ext->{'fm.sessionId'};
    

    But {fm.ack} is empty

    In the decoded JSON string, the false value is not between "".

    Is it possible to access and read the false value and convert it into string value instead?

    Thank you for your helping !

  • mario
    mario over 11 years
    PHP also has a false. Why not compare against that? That's what the decoded JSON array contains.
  • Yogesh Suthar
    Yogesh Suthar over 11 years
    @mario Yeah we can also comapare with false. Thanks for pointing out.
  • LiTHiUM2525
    LiTHiUM2525 over 11 years
    @Yogesh Suthar : i try your code from my other question and your code work very well as is, so when i receive the string on $_POST your code dosen't work, only work with : $receive = json_decode(file_get_contents('php://input')); so i try to add ,true after file_get_contents('php://input'), true) and nothing work if i add ,true...
  • LiTHiUM2525
    LiTHiUM2525 over 11 years
    so if i try : if($receive[0]->ext->{'fm.ack'} == "") { echo "false"; } else { echo "true"; } it's work !!! Thank you !
  • Yogesh Suthar
    Yogesh Suthar over 11 years
    @LiTHiUM2525 true inside json_decode is used for converting all your json data to array format.
  • LiTHiUM2525
    LiTHiUM2525 over 11 years
    @YogeshSuthar yes i know it so is working only if i hardcode my JSon string, not working when i receive JSon string on $_POST the only code can read the string is $receive = json_decode(file_get_contents('php://input')); and not work if i add ,true... so your solution to test if == "" for false else true working well. Thank you
  • Richard Clayton
    Richard Clayton over 9 years
    This is so ghetto. Running into this problem right now. In what world should a valid JSON value of "false" be returned as "blank"?