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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T18:29:43+00:00 2026-06-07T18:29:43+00:00

I am trying to decode some JSON into a php array. Here’s the code

  • 0

I am trying to decode some JSON into a php array. Here’s the code excerpt:

$getfile="{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}";
$arr = json_decode($getfile, true);
$arr['day3'] = $selecter;
echo(print_r($arr));

The only thing that gets returned is ‘1’. I’ve checked JSONLint and it is valid json, so I’m wondering why json_decode is failing. I also tried checking what the array is before adding the day3 key to it, and I still return a ‘1’. Any help is appreciated!

Actual code:

$getfile = "";
    $getfile = file_get_contents($file);
    if ($getfile == "") {
        writeLog("Error reading file.");
    }
    writeLog("getfile is " . $getfile);
    writeLog("Decoding JSON data");
    $arr = json_decode($getfile, true);
    writeLog("Decoded raw: " . print_r($arr));
    writeLog("Editing raw data. Adding data for day " . $day);
    $arr['day3'] = $selecter;
    writeLog(print_r($arr));
    $newfile = json_enconde($arr);
    writeLog($newfile);
    if (file_put_contents($file, $newfile)) {
        writeLog("Wrote file to " . $file);
        echo $newfile;
    } else {
        writeLog("Error writting file");
    }

These are the contents of $file (it’s a text file)

{"fname":"Bob","lname":"Thomas","cascade":"bthomas","loc":"res","place":"home 2"}
  • 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-07T18:29:45+00:00Added an answer on June 7, 2026 at 6:29 pm

    We still don’t know what’s in your file. However if:

    "{"fname":"bob","lname":"thomas","cascade":"bthomas","loc":"res","place":"home 2"}"
    

    Then the extraneous outer double quotes will screw the JSON, and json_decode will return NULL. Use json_last_error() to find out. Might also be a UTF-8 BOM or something else …

    Anyway, the 1 is the result from print_r. print_r outputs directly, you don’t need the echo. Also for debugging rather use var_dump()


    More specifically you would want the print_r output returned (instead of the boolean success result 1) and then write that to the log.

    So use:

          writeLog(print_r($arr, TRUE));
    

    Notice the TRUE parameter.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I came across this problem when trying to decode json into an array ,
I'm trying to decode some JSON and insert the values in MySQL. Here is
I'm trying to get some info out of a decoded JSon string into an
I'm trying to decode a JSON feed containing some Cyrillic characters. Not all of
I'm trying to decode a JSON feed containing some Cyrillic characters. Not all of
Using Javascript. I'm trying to loop through an array encoded with JSON. Here is
Here (given below) is some very simple php parsing multidimensional array stuff I am
I keep getting stuck trying to decode some json I'm getting back from a
I'm having a bit of an issue trying to get some PHP code to
I'm trying to put together something to decode a Json string and not having

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.