1

Alright, I am "kind of" new to stdClass objects in PHP and I am experimenting a bit to get an understanding in exporting data / creating APIs to get information from the database with relevant information.

So, what I want is giving a user access to some information from, lets say a database or XML-file (not everything just some parts). (In this case it's a local sqlite3 file) and I want to give the user the data in JSON-format.

What is it I need help with? For short - I would like to get some advice from you to solve the "problem" in a smarter way than I already got. (I guess my way is kind of... ugly solution.

This is the current export of the structure that I export (without JSON-encode): http://codepaste.net/965tgj

I have done this by the following way in a php file: (The $users is acually data from sqlite3.db and I do understand that to the fullest how to use, so I slimmed the code down :)

$users = array(
    array(
        "id" => 1,
        "user" => "Xavizus"
    ),
    array(
        "id" => 2,
        "user" => "Zariel"
    )
);

$obj = new stdClass();
$obj->auth = true;
$obj->usercount = 2;
$obj->users = array();

for($i =0; $i < $obj->usercount; $i++){
        $obj->users[] = new stdClass();
}

for($i = 0; $i < $obj->usercount; $i++) {
        $obj->users[$i]->id = $users[$i]['id'];
        $obj->users[$i]->user = $users[$i]['user'];
}

print_r($obj);

Is there a cleaner way to do this? or is this as cleanest I can go for?

3
  • 1
    You should define your own class and have the constructor accept initialization values. That would be a lot cleaner. Commented Apr 7, 2015 at 18:43
  • Be sure to get your stdClasses tested... Commented Apr 7, 2015 at 18:52
  • @jeroen Thank you for the tip :) (As I said before - I am kind of new in PHP and I am still learning - I got to say - This is an awesome community! Commented Apr 7, 2015 at 19:05

2 Answers 2

2

This would be easier using (object) to cast the array:

$obj = new stdClass();
$obj->auth = true;
$obj->usercount = count($users);
$obj->users = $users;

foreach($obj->users as &$user) {
    $user = (object)$user;
}

However I would look into @jeroen's comment: "You should define your own class and have the constructor accept initialization values. That would be a lot cleaner."

class users {

    function __construct($users, $auth=true) {
        $this->auth = $auth;
        $this->usercount = count($users);        

        foreach($users as $user) {
            $this->users[] = (object)$user;
        }
    }
}

$obj = new users($users);

You could also cast the row array to an object when assigning it from the fetchArray().

Sign up to request clarification or add additional context in comments.

2 Comments

Thank you for the the excellent explanation :) Didn't really expect that this community was this helpful :)
Okey - I don't find the reputation button.... Edit: Nvm.... Just needed to press the answer button and answer vote :)
1

you can do it in one for loop right?

instead of

for($i =0; $i < $obj->usercount; $i++){
        $obj->users[] = new stdClass();
}

for($i = 0; $i < $obj->usercount; $i++) {
        $obj->users[$i]->id = $users[$i]['id'];
        $obj->users[$i]->user = $users[$i]['user'];
}

you can do it something like

for($i =0; $i < $obj->usercount; $i++){
        $newObject = new stdClass();
        $newObject->id = $users[$i]['id'];
        $newObject->user = $users[$i]['user'];
        $obj->users[] = $newObject;
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.