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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T00:55:13+00:00 2026-06-10T00:55:13+00:00

In PHP, I have a product object that contains a collection of attributes. json_encode

  • 0

In PHP, I have a product object that contains a collection of attributes. json_encode produces this:

{"id":"123","name":"abc","attributes":{"attributes":[{"key":"sku","value":"xyz"}]}}

“attributes” listed twice is redundant. What’s the best way of structuring object collections so that the json is clean?

class Product {

    public $id;
    public $name;
    public $attributes;

    public function __construct()
    {
        $this->attributes = new Attributes();
    }

    public function get($id)
    {
        $this->id = "123";
        $this->name = "abc";
        $attribute = new Attribute("sku", "xyz");
        $this->attributes->add($attribute);
    }
}

class Attributes
{
    public $attributes;

    public function __construct()
    {
        $this->attributes = array();
    }

    public function add($attribute)
    {
        array_push($this->attributes, $attribute);
    }
}

class Attribute
{
    public $key;
    public $value;

    public function __construct($key, $value)
    {
        $this->set($key, $value);
    }
}
  • 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-10T00:55:14+00:00Added an answer on June 10, 2026 at 12:55 am

    I would just use an associative array.

    class Product {
    ...
        public $attributes=array();
        ...
        public function get($id)
        {
            ...
            $this->attributes["sku"]="xyz";
            $this->attributes["foo"]="bar";
        }
    }
    

    json_encode() should produce something like this:

    {"id":"123","name":"abc","attributes":{"sku":"xyz","foo":"bar"}}
    

    OR using variable variables:

    class Attributes
    {
        public function add($key,$value)
        {
            $this->{$key}=$value;
        }
        public function drop($key)
        {
            unset($this->{$key});
        }
    }
    
    $a=new Attributes();
    $a->add('sku','xyz');
    $a->add('foo','bar');
    echo json_encode($a).'<br>';
    $a->drop('sku');
    echo json_encode($a).'<br>';
    

    Output:

    {"sku":"xyz","foo":"bar"}
    {"foo":"bar"}
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a custom class object in PHP named product : final class product
I have an object-oriented parent-child tree in PHP that I want to clone. The
Ok, I have this php page that makes an searchItem request at amazon and
I have a product catalog using apache, php and mysql. I need to put
I have some basic PHP code: $raceramps56[short] = My Test Product; $leftMenu = '<div
PHP: I have made up a function that returns an array. I would like
I have php code that puts some values into an Array as follows: $hunter=addslashes($MessageArray[1]);
I have PHP script works without problems, like this : while($row4 = mysql_fetch_array($result4)) {
I have PHP snippets like this ( snippet.php ): <?php $some_param = some_function(); ?>
Ok I have a string... $a_string = Product; and I want to use this

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.