2012-02-11 53 views
1

嘿,我已经得到的数据在我的变量($报告& REP)使用的foreach循环通过将数据从数据库

注意到它正从4个不同的桌上的东西在数据库中现在

当我打印我的$ rep我得到以下内容:

Array 
(
    [Report] => Array 
     (
      [id] => 246 
      [emp] => werock 
      [name] => werock 
      [organization] => cakephp 
      [customer] => great 
     ) 

    [file] => Array 
     (
      [0] => Array 
       (
        [id] => 211 
        [report_id] => 246 
        [file_name] => 
        [file_type] => 
        [file_size] => 0 
        [file_error] => 4 
        [file_tag] => 0 
       ) 

     ) 

    [Engineer] => Array 
     (
      [0] => Array 
       (
        [id] => 232 
        [report_id] => 246 
       ) 

     ) 

    [Issue] => Array 
     (
      [0] => Array 
       (
        [id] => 118 
        [report_id] => 246 
        [date_created] => 2012-02-10 
        [status] => wait 
       ) 

      [1] => Array 
       (
        [id] => 119 
        [report_id] => 246 
        [date_created] => 2012-02-10 
        [status] => debug 
       ) 

      [2] => Array 
       (
        [id] => 120 
        [report_id] => 246 
        [date_created] => 2012-02-10 
        [status] => Completed 

       ) 

     ) 

) 

现在我想要做的是访问Issues数组并检查其中有多少个数组。在这种情况下是3(0,1,2)。并在这种情况下打印最后一个索引的状态值(2)。

但是,当我做$ rep ['问题'] ['状态']我得到未定义的索引:状态。你能告诉我我哪里可能会出问题吗?

回答

1

这个怎么样:

echo $rep['Issue'][count($rep['Issue'])-1]['status']; 

让我知道,如果它的工作原理。

0

试试这个

$rep['Issue'][$x]['status'] 

其中$ x是可变的,你可以在一个循环中使用得到的值。

0

您错过了第二个索引。它应该是:

$rep['Issue'][count($rep['Issue'])-1]['status']

您也可以尝试使用Set::extract代替。 Set类对于处理数组非常有用。

$status = Set::extract('/Issue/.[:last]/status', $rep); 
if(count($status)){ 
// $status[0] == the value of status which is 'Complete' in your example 
} else { 
// no issues 
} 
+0

set :: extract实际上是获取第一条记录而不是最后一条记录。 – 2012-02-12 01:59:57

+0

$ rep ['Issue'] [count($ rep ['Issue']) - 1] ['status']这工作..... – 2012-02-12 02:02:55

+0

你确定吗?我昨天用你发布的数组测试了这个代码,Set :: extract提取了最后一个记录(即$ status [0] =='Complete')。 – 2012-02-12 02:15:22

-1

我试过另一种方法,如果只是想共享。

$ endEl [$ rep ['Report'] ['id']] = end($ rep ['Issue']);

让我知道它是否是一个好方法..它的工作原理