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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T15:54:38+00:00 2026-06-05T15:54:38+00:00

Our PHP application makes use of json_encode($myObject) a lot, in conjunction with the mustache

  • 0

Our PHP application makes use of json_encode($myObject) a lot, in conjunction with the mustache template library. It’s awesome.

Trouble is, when returning json data from an ajax request it reveals the whole stucture of our objects, even if we don’t have data assigned to them. A simple example:

Fetch a user via ajax and let the server return the object with json_encode($user)

The json:

"_userID":"1","_password":null,"_avatar":"0000723_8949d1d7eb0214fdf6c1af3cb6b66ed3_a.jpg","_blocked":null,"_confirmed":null,"_registerDate":null,"_lastVisitDate":null,"_template":null,"_referrerUserID":null,"_referrerURL":null,"_referrerDomain":null,"_referrerSearchTerms":null,"_confirmationHash":null,"_type":"Administrator" and so on…

It reveals a lot about our objects when all I wanted to return was just a few fields.

Obviously I could rewrite our server side code to send back an array or different objects which are more limited but really that makes life harder and sort of prevents our clean template design which handles the same objects as our server does.

How do I clear all null properties from a json_encode. Does anybody else have this issue and a nice and clean solution?

  • 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-05T15:54:40+00:00Added an answer on June 5, 2026 at 3:54 pm

    Thanks to @gion_13, i’ve adapted his code and come up with a full solution:

    $output = array('data'=>$data,'template'=>$template);
    $output = object_unset_nulls($output);
    echo json_encode($output);
    
    
    function object_unset_nulls($obj)
      {
        $arrObj = is_object($obj) ? get_object_vars($obj) : $obj;
        foreach($arrObj as $key => $val)
        {
          $val = (is_array($val) || is_object($val)) ? object_unset_nulls($val) : $val;
          if (is_array($obj))
            $obj[$key] = $val;
          else
            $obj->$key = $val;
          if($val == null)
            unset($obj->$key);
        }
        return $obj;
      }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I want to allow uploads of very large files into our PHP application (hundred
In a load test of our PHP based web application we can easily reach
I am developing a PHP application using our XAMPP setup as a test server.
I'm developing a PHP application in a IDE, and want to use Git for
Internally our PHP application uses UTF-8, and we do processing on .csv files and
We are using Jmeter to test our Php application running on the Apache 2
We need to have our PHP application hosted on a 64 bit Windows OS
Our PHP scripts create dynamic folders and files and we are having a permission
I just received an email from our host and they've upgraded our PHP to
We currently have an error catching script for all our PHP sites. This 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.