Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8466933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T15:29:31+00:00 2026-06-10T15:29:31+00:00

In PHP, I noticed that if I have an array and then json_encode() it,

  • 0

In PHP, I noticed that if I have an array and then json_encode() it, the boolean values get converted to true and false. However, I want them to be converted to 1 and 0, respectively.

Here’s an example:

$data = Array("foo" => true, "bar" => false, "baz" => false, "biz" => true);
print json_encode($data);

The above outputs:

{"foo":true,"bar":false,"baz":false,"biz":true}

However, if true and false were 1 and 0 instead, we could have a shorter string, which would take less time to transfer over the Internet:

{"foo":1,"bar":0,"baz":0,"biz":1}

How can I make PHP encode JSON using 1 and 0 instead of true and false?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-10T15:29:33+00:00Added an answer on June 10, 2026 at 3:29 pm

    I figured it out. You can use the array_walk or array_walk_recursive function in PHP to cast the booleans to integers before encoding the JSON. I wrote a function to do that:

    function change_booleans_to_numbers(Array $data){
        // Note the order of arguments and the & in front of $value 
        function converter(&$value, $key){
            if(is_bool($value)){
                $value = ($value ? 1 : 0);
            }
        }
        array_walk_recursive($data, 'converter');
        return $data;
    }
    

    Here’s a demonstration script:

    <?php
    // Make the browser display this as plain text instead of HTML 
    header("Content-Type:text/plain");
    
    function change_booleans_to_numbers(Array $data){
        function converter(&$value, $key){
            if(is_bool($value)){
                $value = ($value ? 1 : 0);
            }
        }
        array_walk_recursive($data, 'converter');
        return $data;
    }
    
    $data = Array("foo" => true, "bar" => false, "baz" => false, "biz" => true);
    
    print "Original:" . PHP_EOL;
    var_dump($data);
    print json_encode($data) . PHP_EOL;
    print PHP_EOL;
    
    $changed = change_booleans_to_numbers($data);
    print "Processed:" . PHP_EOL;
    var_dump($changed);
    print json_encode($changed) . PHP_EOL;
    

    The script outputs:

    Original:
    array(4) {
      ["foo"]=>
      bool(true)
      ["bar"]=>
      bool(false)
      ["baz"]=>
      bool(false)
      ["biz"]=>
      bool(true)
    }
    {"foo":true,"bar":false,"baz":false,"biz":true}
    
    Processed:
    array(4) {
      ["foo"]=>
      int(1)
      ["bar"]=>
      int(0)
      ["baz"]=>
      int(0)
      ["biz"]=>
      int(1)
    }
    {"foo":1,"bar":0,"baz":0,"biz":1}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: php false place in condition I have noticed that a lot of
I want to send files through php using readfile() What i've noticed is that
What I have noticed is that responses from AWS PHP SDK are sometimes a
I have an array that has been serialized by php, the result is: unserialize('a:2:{s:13:custom_basket;a:1:{i:280583837398;a:4:{s:12:product_name;s:0:;s:8:quantity;s:1:1;s:5:price;d:38.649999999999999;s:11:description;a:7:{s:2:id;s:12:280583837398;s:3:sku;s:0:;s:4:site;s:2:UK;s:12:condition_id;s:4:1000;s:14:transaction_id;s:12:773563256018;s:8:platform;s:4:eBay;s:18:order_line_item_id;s:25:280583837398-773563256018;}}}s:6:basket;a:0:{}}')
I have noticed that many websites use urls that end in website.com/index.php?var=value&var2=value2 and I
After using PHP for a while now, I've noticed that not all built-in PHP
I am using PHP mail() to send daily notice emails. But I noticed that
I have noticed in the PHP regex library there is a choice between ereg
I have a bit of PHP code that grabs a list of files from
I noticed that some arrays don't have a comma after the last item. I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.