dppx9253 2012-04-12 05:28
浏览 41
已采纳

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

How to count elements of a nested array? I want to count elements of $student_info['Aptitude'].

I tried the following and I'm getting undefined index at the following code:

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 is an array within $student_info which has 4 arrays

var_dump($student_info) produces the following output. Aptitude array is on the last line. It doesn't have any elements:

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) { 
        } 
    } 
} 

thank you.

  • 写回答

3条回答 默认 最新

  • dongzang7182 2012-04-12 05:37
    关注
    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'));
    

    you forgot the [0] index.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(2条)

报告相同问题?

悬赏问题

  • ¥15 求差集那个函数有问题,有无佬可以解决
  • ¥15 【提问】基于Invest的水源涵养
  • ¥20 微信网友居然可以通过vx号找到我绑的手机号
  • ¥15 寻一个支付宝扫码远程授权登录的软件助手app
  • ¥15 解riccati方程组
  • ¥15 display:none;样式在嵌套结构中的已设置了display样式的元素上不起作用?
  • ¥15 使用rabbitMQ 消息队列作为url源进行多线程爬取时,总有几个url没有处理的问题。
  • ¥15 Ubuntu在安装序列比对软件STAR时出现报错如何解决
  • ¥50 树莓派安卓APK系统签名
  • ¥65 汇编语言除法溢出问题