2012-07-06 77 views
1

我想這應該是smple,但我一直堅持這一段時間,所以我需要幫助。我有一個CakePHP程序,我得到下面的數組:CakePHP使用返回的數組值

chratSortedMean= array(
    'totalAucFourHour' => array(
     'Screening Visit-1 (scr)' => array(
      'MmttFourthHour' => array(
       'totalAucs' => array(
        (int) 0 => (float) 0, 
        (int) 1 => (float) 220 
       ) 
      ) 
     ), 
     'Month12' => array(
      'MmttFourthHour' => array(
       'totalAucs' => array(
        (int) 0 => (float) 263.25, 
        (int) 1 => (float) 16.25 
       ) 
      ) 
     ) 
    ), 
    'totalAucTwoHour' => array(
     'Day0' => array(
      'MmttSecondHour' => array(
       'totalAucs' => array(
        (int) 0 => (float) 0, 
        (int) 1 => (float) 112.125 
       ) 
      ) 
     ), 
     'DayD2' => array(
      'MmttSecondHour' => array(
       'totalAucs' => array(
        (int) 0 => (float) 97.375, 
        (int) 1 => (float) 4.5 
       ) 
      ) 
     ), 
     'Month3' => array(
      'MmttSecondHour' => array(
       'totalAucs' => array(
        (int) 0 => (float) 8.53125, 
        (int) 1 => (float) 8.75 
       ) 
      ) 
     ) 
    ) 
) 

我要計算每個時間點的totalAucs值的平均值(篩選拜訪-1(SCR),Month12,Day0等)分別爲總AucFourHour和totalAucTwoHour單獨。

所以我要的結果是有些下面的形式:

'totalAucFourHour' => array(
     'Screening Visit-1 (scr)' => array(
      'MmttFourthHour' => array(
       'totalAucMean' => (float) 110 
       ) 
      ) 
     ), 
       'Month12' => array(
      'MmttFourthHour' => array(
       'totalAucMean' => (float) 139.75 

       ) 
      ) 
     ) 
    ), 

等。我不希望使用多個for循環,因爲我預計數據將來會增長。

回答

0

CakePHP具有核心實用程序類,其中一個名爲Set。設置是數組操作 類。

Set::methodNane(); 

你需要什麼extract();classicExtract();方法:你可以從任何地方在你的應用程序與調用它。用法相當簡單:

$Day0path = '/chratSortedMean/totalAucTwoHour/Day0/MmttSecondHour' 
$arrayOfDay0s = Set::extract($Day0path, $data); 

檢查Cake Book的其他可能性。此外,自CakePHP 2.2以來,有一個新的數組操作類Hash,它更快。由於該版本Set實際上已被棄用,但是it will not be removed up until Cake 3.0