访问关联数组:为什么这个(可能是不正确的)代码有效?

时间:2015-04-24 04:57:27

标签: php arrays associative-array

问题:

在第二个(以下)代码段中,在内部 foreach循环(即嵌套在另一个foreach循环中的foreach循环)中, $value对应$studentCoursesStudied的元素,而不是$studentCoursesStudied数组本身,对吗?

correct way访问$studentCoursesStudied数组元素的值将是:

$studentCoursesStudied[$key]

但他们正在访问$studentCoursesStudied数组的每个元素的值,如

$value['an-element-name/key']

其中$ value对应于数组的元素,而不是数组本身!为什么?

背景: 我正在阅读由其他人编写的PHP脚本进行学习,我不明白他们是如何在某一点上访问关联数组中的值的。所以我写了这个示例代码来问这个问题。

这是一个Student类,学生有一个包含关联数组的属性,名称为recordCoursesStudied

class Student {
    public $studentName;
    public $studentAge;
    public $studentCoursesStudied = null;

    function __construct($studentName, $studentGpa) {
        $this->studentName = $studentName;
        $this->studentGpa = $studentGpa;
    }

    function recordCoursesStudied ($courseName, $courseNumber, $creditHours, $maxMarks, $courseType) {
        $this->studentCoursesStudied[] = array(
            'courseName' => $courseName;
            'courseNumber' => $courseNumber;
            'creditHours' => $creditHours;
            'maxMarks' => $maxMarks;
            'courseType' => $courseType;
        );  
    }
} 

然后,我们希望从方法recordCoursesStudied访问另一个类Whatever中的newCourseRecord元素。

注意: 在内部foreach循环之后的块注释中,我编写了相同的片段,其中包含更好的名称以便清楚地表达。

class Whatever {
    private $students;
    private $currentStudent;

    function __construct () {
        $this->newStudent('Zarah', 3.4);
        $this->newCourseRecord('DBMS', 'BR223', 3, 100, 'Core');
        $this->doTheWork();
    }

    function newStudent($studentName, $studentGpa) {
        $this->students[$studentName] = new Student($studentName, $studentGpa);
        $this->currentStudent = $studentName;
    }

    function newCourseRecord($courseName, $courseNumber, $creditHours, $maxMarks, $courseType) {
        $this->students[$this->studentName]->recordCoursesStudied($courseName, $courseNumber, $creditHours, $maxMarks, $courseType);
    }

    function doTheWork() {
        ...
        foreach ($this->students as $key=>$value) {
            ...
            foreach($value->studentCoursesStudied as $key=>$value) { /****************************/
                ...
                //Echo each property of $studentCoursesStudied.
                someOtherFunction($value['courseName'], $value['courseNumber'], $value['creditHours'], $value['maxMarks'], $value['courseType']);
                ...
            }
            ...
        }
        /*foreach ($this->students as $studentKey=>$studentValue) {
            ...
            foreach($studentValue->studentCoursesStudied as $courseKey=>$courseValue) { /****************************/
                ...
                //Echo each property of $studentCoursesStudied.
                /*someOtherFunction($courseValue['courseName'], $courseValue['courseNumber'], $courseValue['creditHours'], $courseValue['maxMarks'], $courseValue['courseType']);
                ...
            }
            ...
        }*/
        ...
    }

}

在给定的代码中,在foreach突出显示的*循环中,他们通过执行以下操作访问了Student::studentCoursesStudied数组中的值:

$value-of-an-element-in-array['key-of-an-element-in-array']

但不应该像name-of-array['key-of-an-element-in-array']那样访问关联数组的元素吗?

3 个答案:

答案 0 :(得分:2)

你确定方法recordCoursesStudied不是这样吗?

$this->studentCoursesStudied[] = array(
        'courseName' => $courseName;
        'courseNumber' => $courseNumber;
        'creditHours' => $creditHours;
        'maxMarks' => $maxMarks;
        'courseType' => $courseType;
    ); 

如果studentCoursesStudied是一个包含多个课程信息的数组(并且每个数组项都是描述单个课程的字段数组),那将是有意义的。然后你的嵌套foreach会有意义,除非你不能使用$ key和$ value作为循环变量。

答案 1 :(得分:1)

您构建第一个类并在

中设置值
$this->students[$studentName] = new Student($studentName, $studentGpa);

 function newCourseRecord(
                 $courseName, 
                 $courseNumber, 
                 $creditHours, 
                 $maxMarks, 
                 $courseType) {
                    $this->students[$this->studentName]->recordCoursesStudied(
                           $courseName, 
                           $courseNumber, 
                           $creditHours, 
                           $maxMarks, 
                           $courseType);
 }

比访问循环中的值

    foreach ($this->students as $key=>$value) {
        ...
        foreach($value->studentCoursesStudied as $key=>$value) { /****************************/
            ...
            //Echo each property of $studentCoursesStudied.
            someOtherFunction($value['courseName'], $value['courseNumber'], $value['creditHours'], $value['maxMarks'], $value['courseType']);
            ...
        }
        ...
    }

这是正确的。

答案 2 :(得分:0)

尝试以下:

class Student {
private $studentName;
private $studentAge;
private $studentCoursesStudied = null; //private  not run out this class so you can define a public function to get it

public function getStudentCoursesStudied() {
  return $this -> $studentCoursesStudied;
}

function __construct($studentName, $studentGpa) {
    $this->studentName = $studentName;
    $this->studentGpa = $studentGpa;
}

function recordCoursesStudied ($courseName, $courseNumber, $creditHours, $maxMarks, $courseType) {
    $this->studentCoursesStudied = array(
        'courseName' => $courseName;
        'courseNumber' => $courseNumber;
        'creditHours' => $creditHours;
        'maxMarks' => $maxMarks;
        'courseType' => $courseType;
    );  
}
} 


class Whatever {
private $students = array(); // define is a empty array();
private $currentStudent;

function __construct () {
    $this->newStudent('Zarah', 3.4);
    $this->newCourseRecord('DBMS', 'BR223', 3, 100, 'Core');
    $this->doTheWork();
}

function newStudent($studentName, $studentGpa) {
    $this->students[$studentName] = new Student($studentName, $studentGpa);
    $this->currentStudent = $studentName;
}

function newCourseRecord($courseName, $courseNumber, $creditHours, $maxMarks, $courseType) {
//you use $this -> studentName but i n't find where's defined i think you want to use $this -> currentStudent
    $this->students[$this-> currentStudent]->recordCoursesStudied($courseName, $courseNumber, $creditHours, $maxMarks, $courseType); 
}

function doTheWork() {
    ...
    foreach ($this->students as $key=>$value) {
        ...
        foreach($value-> getStudentCoursesStudied() as $key=>$value) { /****************************/
            ...
            //Echo each property of $studentCoursesStudied.
            someOtherFunction($value['courseName'], $value['courseNumber'], $value['creditHours'], $value['maxMarks'], $value['courseType']);
            ...
        }
        ...
    }
    /*foreach ($this->students as $studentKey=>$studentValue) {
        ...
        foreach($studentValue->studentCoursesStudied as $courseKey=>$courseValue) { /****************************/
            ...
            //Echo each property of $studentCoursesStudied.
            /*someOtherFunction($courseValue['courseName'], $courseValue['courseNumber'], $courseValue['creditHours'], $courseValue['maxMarks'], $courseValue['courseType']);
            ...
        }
        ...
    }*/
    ...
}

}