2015-04-24 48 views
0

問題:訪問關聯數組:爲什麼這個(可能是不正確的)代碼工作?

在第二(以下)的代碼段,在foreach循環(也就是foreach循環嵌套在另一個foreach循環),則$value對應於$studentCoursesStudied的元件,而不是$studentCoursesStudied數組本身,對吧?

correct way訪問$studentCoursesStudied陣列的元素的值將是:

$studentCoursesStudied[$key] 

但是他們正在訪問像

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

其中$studentCoursesStudied陣列的每個元素的值$ 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; 
     ); 
    } 
} 

然後我們想從另一個類Whatever從它的方法newCourseRecord訪問recordCoursesStudied的元素。

注:在塊註釋內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']

+0

我有點困惑,因爲你[不應該能夠訪問那個私人財產](https://eval.in/318112)直接 –

+0

@scrowler嘿,我只是寫這個匆忙演示問題。我將更改訪問修飾符(但也可能有其他一些小錯誤) – Solace

+0

您在第二課中有錯誤。你還沒有在'newCourseRecord'方法中設置'$ this-> studentName'。你最好發貼原始代碼 – winston86

回答

2

確定方法recordCoursesStudied不是這個樣子的?

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

這將是有意義如果studentCoursesStudied用有關多個課程的信息的陣列(和每個數組項是描述單個當然字段的數組)。那麼你的嵌套foreach將有意義,除了你不能使用$ key和$ value作爲循環變量。

+0

是的,我錯過了'[]'。這是一個錯字 - 它是否有很大的不同? – Solace

+1

它在這個多維數組中創建了另一個層次。像'$ a [] = $ b'這樣的語句將$ b作爲最後一項放入一個數組$ a中,如下所示:'array($ b)'。然後如果你做'$ a [] = $ c' $ a將變成'array($ b,$ c)'。在你的情況$ this-> studentCoursesStudied會變成這樣的:'array(array('courseName'=>'course1',...),array('courseName'=>'course2',...),。 ..)' –

1

您構建一流的,比你在循環

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']); 
      ... 
     } 
     ... 
    } 

這是正確的訪問值

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

設置的值比

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

+0

這裏'$ value'對應於'$ studentCoursesStudied'數組中每個'element'的值,而不是'$ studentCoursesStudied'本身,對吧?他們不應該像'$ studentCoursesStudied [$ key]'那樣訪問數組中的元素嗎?我的問題是關於用於訪問數組元素的語法 – Solace

+1

Hello @Zarah。你的代碼和你的問題很混亂,因爲每個循環的foreach變量都有相同的名字。這段代碼不會工作。但看到我的答案,也許我猜對了你想說的話) –

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']); 
      ... 
     } 
     ... 
    }*/ 
    ... 
} 

}

相關問題