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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 18, 20262026-05-18T09:31:02+00:00 2026-05-18T09:31:02+00:00

I am returning a json_encode() of an array of objects pulled from an ORM.

  • 0

I am returning a json_encode() of an array of objects pulled from an ORM. It includes lots of properties with a null value. What is the neatest way to remove these properties that are null? I guess I could iterate over the properties, look if they are null and then unset() that property, but surely there must be a more elegant way?

  • 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-05-18T09:31:03+00:00Added an answer on May 18, 2026 at 9:31 am

    Try this; it will only work on a simple object, but if it’s coming from an ORM it should be simple enough.

    // Strips any false-y values
    $object = (object) array_filter((array) $object);
    

    Thanks to Gordon’s answer to another question yesterday for giving me the idea.

    This works by

    • converting the object to an associative array, where object properties are the keys and their values are the array values
    • using array_filter with default arguments to remove array entries with a false (e.g. empty, or null) values
    • converting the new array back to a simple object

    Note that this will remove all properties with empty values, including empty strings, false boolean values and 0s, not just nulls; you can change the array_filter call if you want to keep those and only remote those that are exactly null.

    // Strips only null values
    $object = (object) array_filter((array) $object, function ($val) {
        return !is_null($val);
    });
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Is there a way to keep json_encode() from returning null for a string that
Instead of returning a common string, is there a way to return classic objects?
I have some code where I'm returning an array of objects. Here's a simplified
I am returning a JSON string from PHP: <?php $results = array( result =>
Is there a way to configure what JSON serializer is used when returning JSON
I am returning XML data from the Yahoo GeoPlanet web service using HttpWebRequest .
I am returning a List from my WCF method. In my client code, it's
How do I go about returning json data from a bottle request handler. I
I am returning results from a MySql database in PHP that I would like
Having problems returning a list of Japanese terms from an MSSql database as JSON.

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.