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

前端 未结 3 1273
感情败类
感情败类 2020-12-21 04:46

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

[
    {
        \"clientId\":\         


        
相关标签:
3条回答
  • 2020-12-21 04:56

    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)
    }
    
    0 讨论(0)
  • 2020-12-21 05:03

    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";   
    }
    
    0 讨论(0)
  • 2020-12-21 05:21

    It's a very simple. If you use js to generate and transfer json, pass your variable not in its pure form but: youBoolVar + 0, then false will be 0, and true 1

    0 讨论(0)
提交回复
热议问题