繁体   English   中英

php cakephp:如何计算嵌套数组的元素

[英]php cakephp: how to count elements of a nested array

如何计算嵌套数组的元素? 我想计算$ 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是$ 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) "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) { 
        } 
    } 
} 

谢谢。

采用

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

代替。 请在下次自己格式化您的示例代码。 你自己很容易发现这个错误。

考虑使用xdebugvar_dump提供更易读的输出。

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]索引。

尝试这个:

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM