1

ネストされた配列の要素を数える方法は? $student_info['Aptitude'] の要素を数えたい。

私は次のことを試しましたが、次のコードで未定義のインデックスを取得しています:

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

Aptitude は、4 つの配列を持つ $student_info 内の配列です

var_dump($student_info) は、次の出力を生成します。適性配列は最後の行にあります。要素はありません:

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) "kpalani@streamyx.com" 
            ["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) { 
        } 
    } 
} 

ありがとうございました。

4

3 に答える 3

2

使用する

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

代わりは。次回はサンプルコードを自分でフォーマットしてください。この間違いを自分で簡単に見つけることができたはずです。

xdebugを使用して、の出力をより読みやすくすることを検討してくださいvar_dump

于 2012-04-12T05:36:57.900 に答える
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]インデックスを忘れました。

于 2012-04-12T05:37:48.617 に答える