0

I have JSON:

[{"name":"point","visibility":false},{"name":"php_first_table","visibility":true}]

I do:

$Arr = json_decode($json,true);

I get:

Array
 (
  [0] => Array
    (
        [name] => point
        [visibility] => 
    )

  [1] => Array
    (
        [name] => php_first_table
        [visibility] => 1
    )

)

How to save true and false in boolean form?

0

3 Answers 3

8

It already is in boolean form. Try to use var_dump($array[0]['visibility']); and it'll output bool(true) or bool(false).

print_r automatically converts the boolean to 1 for true and (empty) for false when outputting, but it doesn't change the data type.

You can use var_dump on your array to get a better output:

<?php
    $array = array(
        'booleanTrue' => true,
        'booleanFalse' => false,
        'integer' => 1
    );
    var_dump($array);
    print_r($array);

    /*
        array(3) {
          ["booleanTrue"]=>
          bool(true)
          ["booleanFalse"]=>
          bool(false)
          ["integer"]=>
          int(1)
        }
        Array
        (
            [booleanTrue] => 1
            [booleanFalse] => 
            [integer] => 1
        )
    */
?>

DEMO

Edit:

Here's a function to give you a nicer/more compact var_dump output very similar to print_r:

<?php
    function var_dump_r($variable, $return = false) {
        ob_start();
        var_dump($variable);
        $output = preg_replace('/\["([^"]+)"\]/', '[$1]', preg_replace('/\s*=>\s*/', ' => ', ob_get_clean()));

        if (!$return) {
            echo $output;
        }
        return $return;
    }

    var_dump_r(array(
        'booleanTrue' => true,
        'booleanFalse' => false,
        'integer' => 1
    ));

    /*
        array(3) {
          [booleanTrue] => bool(true)
          [booleanFalse] => bool(false)
          [integer] => int(1)
        }
    */
?>
Sign up to request clarification or add additional context in comments.

1 Comment

Yeah you are right. Don't know this fact about print_r before. Thanks.
4

It looks fine to me.

Are you using print_r or something similar to print it? (Pro-tip: don't).

Comments

1

TRUE is always a value greater than 0 and FALSE is 0, so you only have to check

if ($visibility) {
   // do something
}

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.