1

how to extend array in parent class in PHP?

I have something like this

class ParentClass {
    $this->player = array(
        'class' => array(
            'basic' => array(
                'name'      => $data['name'],
                'image'     => $data['img']
            )
        )
    );
}

how to extend $this->player array without erasing data in parent class with this data

class ChildClass extends ParentClass {
    $this->player => array(
        'class' => array(
            'loadout'   => array(
                'primary'   => array(
                    'name'      => $data1['name'],
                    'type'      => $data1['type']
                )
            )
        )
    );
}

so the output will be

    (
        'class' => array(
            'basic' => array(
                'name'      => $data['name'],
                'image'     => $data['img']
            ),
            'loadout'   => array(
                'primary'   => array(
                    'name'      => $data1['name'],
                    'type'      => $data1['type']
                )
            )
        )
    )

I have tried this

class ParentClass {
    public $player;

    public function __construct($class) {
        $this->player = array(
            'class' => array(
                'basic' => array(
                    'name'      => $data['name'],
                    'image'     => $data['img']
                )
            )
        );
    }
}

class ChildClass extends ParentClass {
    public function __construct($class) {

        parent::__construct($class);

        $this->player['class'] = array(
            'loadout'   => array(
                'primary'   => array(
                    'name'      => $data1['name'],
                    'type'      => $data1['type']
                )
            )
        );
    }

    public function getData() {
        return $this->player;
    }
}

but it only prints

    (
        'class' => array(
            'loadout'   => array(
                'primary'   => array(
                    'name'      => $data1['name'],
                    'type'      => $data1['type']
                )
            )
        )
    )

1 Answer 1

1

Your construct should look like this:

public function __construct($class) {

    parent::__construct($class);

    $this->player['class']['loadout'] = 
       array(
           'primary'   => array(
               'name'      => $data1['name'],
               'type'      => $data1['type']
            )
      );

}

Your code before overrode class with a new array. This code sets loadout of the array class to the new array.

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

3 Comments

You can make this more general by using array_merge_recursive( $this->player, array('class'=>array('basic'=> ....)))
@NiettheDarkAbsol , I will halt my array_merge answer now and will +1 your answer :D
@wumm why didn't I think of that. That worked like a charm. Thanks a lot

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.