U
U
UrbanRider2014-10-21 13:24:00
PHP
UrbanRider, 2014-10-21 13:24:00

How to access nested array elements in PHP?

Good afternoon.
Please help me understand how to work with nested arrays in PHP.

class skill
{
    public $id=0;
    public $name="";
    public $glyphs=array();

    public function __construct($id,$name)
    {
        $this->id=$id;
        $this->name=$name;
    }
}

class glyph
{
    public $id;
    public $skill_id;
    public $name;
    public $description;
    public $cost;
    public $lvl_req;
    public $icon;

    public function __construct($id,$skill_id,$name,$description,$cost,$lvl_req)
    {
        $this->id=$id;
        $this->skill_id=$skill_id;
        $this->name=$name;
        $this->description=$description;
        $this->cost=$cost;
        $this->lvl_req=$lvl_req;
        $this->icon="/images/glyphs".$name."png";
    }
}

class player
{
    public $class;
    public $level;
    public $skills=array();
    public $points;
}

Explanation: I create 2 helper classes skill and glyph. Moreover, the skill class uses the glyph class as an array of objects. The player class uses the skill class as an array of objects.
How to access from an instance of the player class to access an instance of the glyph class.
Out of habit from C#, this would be done like this:
$player->skills[$i]->glyphs[j];
But in this way I cannot get access.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
A
Alexey, 2014-10-21
@UrbanRider

Everything should work out of habit ideone.com/XEYVFQ

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question