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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T23:48:52+00:00 2026-06-13T23:48:52+00:00

I’m having a very interesting problem with PHP. The following code grabs a line

  • 0

I’m having a very interesting problem with PHP. The following code grabs a line from a text file, decodes that text as json into an stdClass object, then puts it into an array conditionally on one of its properties.

$fileStream = @fopen($fileName, 'r+');
    $lastUpdate = $_POST['lastUpdate'];
    if($fileStream) {
        $eventArray = array();
        while (($buffer = fgets($fileStream, 8192)) !== false) {
                $decodedEvent = json_decode($buffer);
                echo var_dump($decodedEvent);
            if ($decodedEvent->timestamp > $lastUpdate) {
                array_push($eventArray, $decodedEvent);
            }
        }
        $jsonEvents = json_encode($eventArray);
        echo $jsonEvents;
    }
    else {
        $fileStream = @fopen($fileName, 'a');
    }
    @fclose($fileStream);

This produces the error:

Notice:Trying to get property of non-object in C:\****\gameManager.php on line 23

I know that the object is valid in multiple ways. For instance, var_dump is producing this:

object(stdClass)#1 (3) {
 ["name"]=>
 string(4) "move"
 ["args"]=>
 array(3) {
   [0]=>
   int(24)
   [1]=>
   int(300)
   [2]=>
   int(50)
 }
 ["timestamp"]=>
 float(1352223678463)
}

If I try to access $decodedEvent using $decodedEvent["timestamp"] I get an error telling me that objects can’t be accessed as arrays.

Also, it does indeed echo proper json, which can only be encoded from a proper object:

[{"name":"move","args":[24,300,50],"timestamp":1352223678463}]

Am I missing something here, or is PHP misbehaving? Any help is greatly appreciated.

EDIT: Here is the input from the file:

{"name":"move","args":[24,300,50],"timestamp":1352223678463}
  • 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-13T23:48:55+00:00Added an answer on June 13, 2026 at 11:48 pm

    Your JSON format is incorrect. That’s not to say invalid. But given this format the root element is an array of stdClass.

    array(1) {
      [0] =>
      class stdClass#1 (3) {
         // ...
    

    If this is a truly a single object, I would resolve this at the source with the following, proper JSON:

    {"name":"move","args":[24,300,50],"timestamp":1352223678463}
    

    If that is not possible, you need to access it in PHP with proper array notation:

    echo $decodedEvent[0]->timestamp;
    

    UPDATE

    The updated JSON you provided appears valid and correctly formatted given your code. My guess is a line in the file does not contain valid JSON (e.g. empty line) and as such json_decode() is failing which results in the PHP Notice.

    I encourage you to test for this in your loop:

    if ($decodedEvent && $decodedEvent->timestamp > $lastUpdate)
    

    Also bear in mind this is a notice. And while I advocate clean code, it is not an error strictly speaking.

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

Sidebar

Related Questions

I ran into a problem. Wrote the following code snippet: teksti = teksti.Trim() teksti
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
For some reason, after submitting a string like this Jack’s Spindle from a text
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have a text area in my form which accepts all possible characters from
I am currently running into a problem where an element is coming back from
I have a reasonable size flat file database of text documents mostly saved in
I'm trying to create an if statement in PHP that prevents a single post

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.