0

i'm trying to refresh my memory of OO & array structure. i have,

class room{
            private $people = array(
                'name' => array(
                            'height' => null,
                            'age' => null
                        )
            );

            function set($list){
                foreach($list as $person){
                    $this->people[$person['name']]['height'] = $person['height'];
                    $this->people[$person['name']]['age'] = $person['age'];
                }        
            }

            function print(){
                foreach($this->people as $k => $v){
                    echo $k . "<br>";
                    echo $v['height'] . ":" . $v['age'] . "<br><br>";
                }
            }
        }

        $input = array( array('name' => 'John', 'height' => '6.4', 'age' => '20'),
                        array('name' => 'Jane', 'height' => '5.2', 'age' => '21')
            );
        $i = new room;
        $i->set($input);
        $i->print();

the output is,

name
:

John
6.4:20

Jane
5.2:21

i'm confused as why name : appears first, when the input array only contains 2 values of each person. i am unsure if i am using my arrays correctly, could someone point out my mistake?

My overall aim of this is to have correct understanding of arrays within arrays & how to best set & get the values

3 Answers 3

2

It's because you've initialised the $people array to contain those values

private $people = array(
    'name' => array(
        'height' => null,
        'age' => null
     )
);

Change it to:

private $people = array();
Sign up to request clarification or add additional context in comments.

3 Comments

Resolved! i forget that you don't have to define an array in php
i have a 10 minute wait that is preventing me from doing so at the moment :)
you answered too quickly for stackoverflow :P
1

that's the good way to do it your people class

class people {

    //properties
        private $name;
        private $height;
        private $age;

    //setters
        public function setName($name) {
            $this->name = $name;
        }

        public function setHeight($height) {
            $this->height = $height;
        }

        public function setAge($age) {
            $this->age = $age;
        }

    //getters
        public function getName() {
            return $this->name;
        }

        public function getHeight() {
            return $this->height;
        }

        public function getAge() {
            return $this->age;
        }

    }

your room class

    class room {

    //properties
        private $people = array();

    //setters
        public function setPeople($people) {
            $this->people[] = $people;
        }

    //getters
        public function getPeoples() {
            return $this->people;
        }

    }

and how to control it in OOP

    $people1 = new people();

    $people1->setName('John');
    $people1->setHeight('6.4');
    $people1->setAge('20');

    $people2 = new people();

    $people2->setName('Jane');
    $people2->setHeight('5.2');
    $people2->setAge('21');

    $room = new room();

    $room->setPeople($people1);
    $room->setPeople($people2);

Comments

0

// Removing people array initial data will solve the issue :)

class room{ private $people = array();

        function set($list){
            foreach($list as $person){
                $this->people[$person['name']]['height'] = $person['height'];
                $this->people[$person['name']]['age'] = $person['age'];
            }        
        }

        function print(){
            foreach($this->people as $k => $v){
                echo $k . "<br>";
                echo $v['height'] . ":" . $v['age'] . "<br><br>";
            }
        }
    }

    $input = array( array('name' => 'John', 'height' => '6.4', 'age' => '20'),
                    array('name' => 'Jane', 'height' => '5.2', 'age' => '21')
        );
    $i = new room;
    $i->set($input);
    $i->print();

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.