2012-04-12 53 views
0

如何計算嵌套數組的元素?我想要統計$ student_info ['Aptitude']的元素。php cakephp:如何計算嵌套數組的元素

我嘗試以下,我在下面的代碼獲得未定義指數:

else if (count($student_info["Aptitude"])==0){ //no records for report card 
     $this->Session->setFlash('Your child is a new student in our school. 
      He/She doesn\'t have records for a report card yet.'); 
      $this->redirect(array('controller'=>'pages','action'=>'home')); 

性向是內$ student_info具有4個陣列

的var_dump($ student_info)的陣列產生以下輸出。 Aptitude數組在最後一行。它沒有任何元素:

array(1) { 
    [0]=> array(4) { 
     ["Student"]=> array(10) { 
      ["id"]=> string(2) "20" 
      ["name"]=> string(15) "Uma Palaniappan" 
      ["gender"]=> string(6) "Female" 
      ["dob"]=> string(10) "2007-04-26" 
      ["created"]=> string(19) "2011-12-29 10:14:03" 
      ["modified"]=> string(19) "2011-12-29 10:14:03" 
      ["merry_class_id"]=> string(1) "3" 
      ["merry_parent_id"]=> string(2) "25" 
      ["term1_comments"]=> NULL 
      ["term2_comments"]=> NULL 
     } 
     ["MerryParent"]=> array(14) { 
      ["id"]=> string(2) "25" 
      ["initial"]=> string(2) "Mr" 
      ["name"]=> string(13) "Palaniappan K" 
      ["username"]=> string(7) "kpalani" 
      ["email"]=>string(20) "[email protected]" 
      ["password"]=> string(40) "43f5e1298f3b2478a9cd4ab7c6f5f703380dbcc9" 
      ["landline"]=> string(12) "044-77223399" 
      ["mobile"]=> string(10) "9860662309" 
      ["address"]=> string(15) "44 Megala Chowk" 
      ["state_id"]=> string(1) "6" 
      ["city_id"]=> string(3) "103" 
      ["postal_code"]=> string(6) "384733" 
      ["created"]=> string(19) "2011-12-29 10:14:03" 
      ["modified"]=> string(19) "2011-12-29 10:52:23" 
     } 

     ["MerryClass"]=> array(2) { 
      ["id"]=> string(1) "3" 
      ["class_name"]=> string(3) "LKG" 
     } 
     ["Aptitude"]=> array(0) { 
     } 
    } 
} 

謝謝。

+0

大聲笑,希望是測試數據或先生帕拉尼亞潘·K可以得到一些不尋常的電話。當然是 – 2012-04-12 05:38:18

+0

這是測試數據。 :) – vaanipala 2012-04-12 05:57:43

回答

1
else if (count($student_info[0]["Aptitude"])==0){ //no records for report card 
    $this->Session->setFlash('Your child is a new student in our school. 
     He/She doesn\'t have records for a report card yet.'); 
     $this->redirect(array('controller'=>'pages','action'=>'home')); 

您忘記了[0]索引。

1

使用

if (count($student_info[0]["Aptitude"])==0) 

代替。下次請自己設置示例代碼。你本可以很容易地發現這個錯誤。

考慮使用xdebugvar_dump提供更具可讀性的輸出。

0

試試這個:

count(Set::extract('/Aptitude', $student_info))