Php:数组foreach的附加输出


php: additional output from foreach of array

我想刷新我对OO的记忆& &;阵列结构。我,

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();

输出为,

name
:
John
6.4:20
Jane
5.2:21

我很困惑,为什么name :首先出现,当输入数组只包含每个人的2个值。我不确定我是否正确使用我的数组,有人能指出我的错误吗?

我这样做的总体目标是正确理解数组中的数组&如何最好地设置&获取

这是因为您已经初始化了$people数组以包含这些值

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

改为:

private $people = array();

这是一个很好的方法你的人民阶级

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;
        }
    }

以及如何在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);

//删除people数组初始数据将解决问题:)

教室{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();