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

  • Home
  • SEARCH
  • 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 6546653
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T11:44:02+00:00 2026-05-25T11:44:02+00:00

I would like to create the following using class syntax: $resp = new stdclass;

  • 0

I would like to create the following using class syntax:

$resp = new stdclass;

$resp->CategoryListResp->category[0]->categoryId = 1;
$resp->CategoryListResp->category[0]->categoryName = "Spel"; 
$resp->CategoryListResp->category[0]->iconUri = "PictoSpel.png";

$resp->CategoryListResp->category[1]->categoryId = 2;
$resp->CategoryListResp->category[1]->categoryName = "Transport";
$resp->CategoryListResp->category[1]->iconUri = "PictoTransport.png";

Should be easy but I cannot find the syntax for this.
I will later output $resp in json format. I am aware I can also use arrays for this…

The json output shall be:

{"CategoryListResp":{"category":[{"categoryId":1,"categoryName":"Spel","iconUri":"PictoSpel.png"},{"categoryId":2,"categoryName":"Transport","iconUri":"PictoTransport.png"}]}}
  • 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-25T11:44:02+00:00Added an answer on May 25, 2026 at 11:44 am

    You can also make your classes more explicit:

        class Category {
            public $categoryId = 0, $categoryName = '', $iconUri = '';
        }
    
        class Resp {
            public $categoryListResp = null;
            public function __construct() {
                $this->categoryListResp = new CategoryListResp();
            }
        }
    
        class CategoryListResp {
            public $category = array();
        }
    
        $resp = new Resp();
        $resp->categoryListResp->category[0]->categoryId = 1; 
        $resp->categoryListResp->category[0]->categoryName = "Spel";  
        $resp->categoryListResp->category[0]->iconUri = "PictoSpel.png"; 
        // etc.
    

    ADDED (based on henq’s comment). To fully utilize the class concept you would need to add some methods to the classes. Then you would not use -> for arrays, but call the respective methods. E.g.

    class Category {
        public $categoryId = 0, $categoryName = '', $iconUri = '';
        public function __construct($id, $name, $icon) {
            $this->categoryId = $id;
            $this->categoryName = $name;
            $this->iconUri = $icon;
        }
    }
    
    class Resp {
        public $categoryListResp = null;
        public function __construct() {
            $this->categoryListResp = new CategoryListResp();
        }
        public function addCategory($index, $id, $name, $icon) {
            $this->categoryListResp->addCategory($index, $id, $name, $icon);
        }
    }
    
    class CategoryListResp {
        public $category = array();
        public function addCategory($index, $id, $name, $icon) {
            $this->category[$index] = new Category($id, $name, $icon);
        }
    }
    
    $resp = new Resp();        
    $resp->addCategory(0, 1, "Spel", "PictoSpel.png");
    $resp->addCategory(1, 2, "Transport", "PictoTransport.png");
    // etc
    

    You can modify this concept according to your needs.

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

Sidebar

Related Questions

I would like to create/start a simulator for the following microcontroller board: http://www.sparkfun.com/commerce/product_info.php?products_id=707# The
I would like to create a query that returns rows in the following format:
I would like to create a singleton class that is instantiated once in each
Im new to PHP and would like to get some advice on the following
Using the adapter pattern, combined with IoC (specificly Unity), I would like to create
Here's the problem. I would like to create a class which will contain configuration
I would like create my own collection that has all the attributes of python
I would like create a web service in ASP.Net 2.0 that will supports JSON.
Would like to create a strong password in C++. Any suggestions? I assume it
I would like to create a database backed interactive AJAX webapp which has a

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.