0

I have following values,

firstsession
secondsession
thirdsession
B+

i have to convert the above to values to array of object,

stdClass Object
(
    [Itemname] => firstsession
    [Ability] => B+
)
stdClass Object
(
    [Itemname] => secondsession
    [Ability] => B+
)
stdClass Object
(
    [Itemname] => thirdsession
    [Ability] => B+
)

how to do this?

3 Answers 3

1

You can create a small class

<?php

    class Item {
        public $Itemname;
        public $Ability;

        public function __construct($name,$ability) {
            $this->Itemname = $name;
            $this->Ability = $ability;
        }
    }

    $arr = array(
        new Item('firstsession','B+'),
        new Item('secondsession','B+'),
        new Item('thirdsession','B+'),
    );

    print_r($arr);

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

Comments

0

Maybe what you're looking for... http://www.webmaster-source.com/2009/08/20/php-stdclass-storing-data-object-instead-array/

Quick g search, though ;-)

Comments

0
$arr = array();

$a = new stdClass;
$a->Itemname = "first session";
$a->Ability = "B+";

$b = new stdClass;
$b->Itemname = "second session";
$b->Ability = "B+";

$c = new stdClass;
$c->Itemname = "third session";
$c->Ability = "B+";

$arr[] = $a;
$arr[] = $b;
$arr[] = $c;

like this?

2 Comments

You're dealing with plain arrays here. Either you cast your items to objects or you're not achieving what question asks.
ok i've edited my answer. if this doesn't answer the question, it means i didn't get the question. by the way castng an array as stdClass will return an empty object and you will lose the keys and the cursor.

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.