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 9097159
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T00:02:15+00:00 2026-06-17T00:02:15+00:00

I have a PHP script that outputs JSON data which is parsed by Javascript

  • 0

I have a PHP script that outputs JSON data which is parsed by Javascript in Browsers, and also by iPhone and Android apps.

Previously, I found that when PHP converted array data into a JSON, it would make all numberical values into strings as indicated by double quotes. So, for example, "id" : 1 would become "id" : "1". This seemed to confuse some of the Javascript functions that received the JSON data, so I ran this regular expression on the JSON in PHP in order to make sure numbers weren’t made into strings:

$JSONOutput = preg_replace('/"(-?\d+\.?\d*)"/', '$1', json_encode($JSONOutput));

However, this also has the effect of removing double quotes from both keys and values, so 1 : "first value" becomes "1" : "first value". It turns out this is a problem because if JSON keys aren’t strings, then the iPhone app that recieves the data complains.

So, what I want to do is make sure all values in the JSON are preserved as integers, but all keys are made into strings. Is there a way I can run the regular expression I have above on just the values? Or any other process that would give me a similar result?

  • 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-17T00:02:16+00:00Added an answer on June 17, 2026 at 12:02 am

    Is there a way I can run the regular expression I have above on just the values?

    You’re much better off avoiding post-processing the string.

    Previously, I found that when PHP converted array data into a JSON, it would make all numberical values into strings as indicated by double quotes. So, for example, “id” : 1 would become “id” : “1”.

    The answer here is to make sure that the number values really are numbers. If you’re seeing strings with digits in them where you expect numbers, it’s because the value in the PHP object/array is not a number. That’s what you need to fix.

    For example, this code:

    <?php
        header('Content-Type: application/json');
        $x = array(
            "number" => 1,
            "string" => "2"
            );
        echo json_encode($x);
    ?>
    

    …quite correctly produces this output:

    {"number":1,"string":"2"}
    

    Note how the value that’s really a number in the PHP code comes out as a number in the JSON.

    So rather than post-processing the string, the answer is to correct the data you’re feeding into json_encode.


    Replying to your comment below:

    I understand that I could specifically assign string names to keys, but the array in question is generated automatically with a while loop that keeps appending numbers, and those numbers are used in counting operations, so I don’t think this would work in my case.

    It doesn’t make the slightest difference. For example, this code:

    <?php
        header('Content-Type: application/json');
        $x = array();
        for ($n = 1; $n < 5; ++$n) {
           $x["entry" . $n] = $n;
        }
        echo json_encode($x);
    ?>
    

    produces this output:

    {"entry1":1,"entry2":2,"entry3":3,"entry4":4}
    

    Again note that the numbers are numbers in the input, and therefore numbers in the output.

    Also note that PHP correctly handles it if the keys of the object are numbers:

    <?php
        header('Content-Type: application/json');
        $x = array();
        $x["foo"] = "bar";
        for ($n = 1; $n < 5; ++$n) {
           $x[$n] = $n * 2;
        }
        echo json_encode($x);
    ?>
    

    produces

    {"foo":"bar","1":2,"2":4,"3":6,"4":8}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a php script that outputs json data. For the purposes of testing,
I have a proxy script that outputs json data via php, and I want
I have a php script that inserts data from an Android app into a
I have a script that outputs a json string via json_encode in PHP. The
I have two files. location.php, that outputs this: [[javascript],[PHP]] and in another file: <script
I have a PHP script that outputs an array of data. This is then
I have a script in jQuery that retrieves data from http://pi.codele.se/php/debatt.php (json-formatted). And it's
I have a php script that watches for the newest json file in a
Basically I have a php script located on a sever that generates a JSON
I'm building a PHP script that feeds JSON data to another script. My script

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.