2013-11-24 56 views
0

這是一個可能的重複條目。 Unfortunalty我無法從Stackoverflow上的其他條目中找出這一點。所以請幫助,如果你可以:)在多維數組中計數值

我在玩迷你統計腳本在PHP中,顯示一些基本的統計數據。

我有一個多維數組,看起來像這樣:

$found_data = 
array( 
    array('2011-11-02' => 'mobile'), 
    array('2011-11-02' => 'mobile'), 
    array('2011-11-04' => 'mobile'), 
    array('2011-11-08' => 'Desktop'), 
    array('2011-11-08' => 'mobile'), 
    array('2011-11-08' => 'mobile'), 
    array('2011-11-08' => 'mobile'), 
    array('2011-11-15' => 'mobile'), 
    array('2011-11-18' => 'mobile'), 
    array('2011-11-21' => 'Desktop'), 
    array('2011-11-23' => 'mobile'), 
    array('2011-11-28' => 'Desktop'), 
    array('2011-11-30' => 'mobile') 
); 

現在,我要代表這樣的這樣的數據:

2011-11-01: 0 from mobile and 0 from desktop 
2011-11-02: 2 from mobile and 0 from desktop 
... 
2011-11-30: 1 from mobile and 0 from desktop 

我敢肯定你上心。

我已經嘗試了很多的變化以下,但沒有任何工程:

echo count(array_keys($found_data['2011-11-02'], "mobile")); 

任何幫助是非常讚賞:)

回答

0

我已經在不同WY解決它,比所建議的上方。

如果任何一個應該關心,這就是答案:

$found_data = 
array( 
    array('2011-11-02' => 'Mobile'), 
    array('2011-11-02' => 'Mobile'), 
    array('2011-11-04' => 'Mobile'), 
    array('2011-11-08' => 'Desktop'), 
    array('2011-11-08' => 'Mobile'), 
    array('2011-11-08' => 'Mobile'), 
    array('2011-11-08' => 'Mobile'), 
    array('2011-11-15' => 'Mobile'), 
    array('2011-11-18' => 'Mobile'), 
    array('2011-11-21' => 'Desktop'), 
    array('2011-11-23' => 'Mobile'), 
    array('2011-11-28' => 'Desktop'), 
    array('2011-11-30' => 'Mobile') 
); 



foreach($found_data as $single_data) 
{ 
    foreach($single_data as $data => $key) 
    { 
     $array_combined[$data]['Mobile'] = array(); 
     $array_combined[$data]['Tablet'] = array(); 
     $array_combined[$data]['Desktop'] = array();     
    } 
} 


foreach($found_data as $single_data) 
{ 
    foreach($single_data as $data => $key) 
    { 
     if($key=='Desktop') 
      array_push($array_combined[$data]['Desktop'], $key); 
     else if($key=='Tablet') 
      array_push($array_combined[$data]['Tablet'], $key); 
     else if($key=='Mobile') 
      array_push($array_combined[$data]['Mobile'], $key); 

    } 
} 


$startdate = strtotime('2011-11-01 00:00:01'); 
$days = 30; 

echo 'Total hits the last 30 days: '.count($found_data).'<br />'; 

echo 'Desktop hits the last 30 days<br />'; 
for ($i = 0; $i <= $days; $i++) { 
    $date = date('Y-m-d', $startdate+$i*60*60*24); 
    printf("%s\t%s\n", $date, array_key_exists($date, $array_combined) ? count($array_combined[$date]['Desktop']) : 0); 
    echo '<br />'; 
} 

echo 'Tablet hits the last 30 days<br />'; 
for ($i = 0; $i <= $days; $i++) { 
    $date = date('Y-m-d', $startdate+$i*60*60*24); 
    printf("%s\t%s\n", $date, array_key_exists($date, $array_combined) ? count($array_combined[$date]['Tablet']) : 0); 
    echo '<br />'; 
} 

echo 'Mobile hits the last 30 days<br />'; 
for ($i = 0; $i <= $days; $i++) { 
    $date = date('Y-m-d', $startdate+$i*60*60*24); 
    printf("%s\t%s\n", $date, array_key_exists($date, $array_combined) ? count($array_combined[$date]['Mobile']) : 0); 
    echo '<br />'; 
} 

Propably不是最好的解決辦法,但作爲intented工作。

2

你不能索引你的$ found_data這樣的:$found_data['2011-11-02'],因爲你有那裏沒有名爲「2011-11-02」的鑰匙。您$ found_data聲明等效於:

$found_data = 
array( 
    0 => array('2011-11-02' => 'mobile'), 
    1 => array('2011-11-02' => 'mobile'), 
    2 => array('2011-11-04' => 'mobile'), 
    ... // and so on 
); 

我知道你正在使用多維使用相同的密鑰多次,但如上面所示的結構,你不能簡單地用array_keys。得到你想要的東西

的一種方法是:

  1. 創建一個結果變量,初始化爲空數組
  2. 獲取下一個項目在$ found_data
  3. 獲得該項目
  4. keyvalue
  5. 檢查結果是否存在key,如果是,則分別增加value。否則,創建一個新陣列,其中mobiledesktop作爲關鍵字,並使用value來確定哪一個應該用1(其他爲0)進行初始化。分配該陣列以產生與主要key

預期的結果陣列結構是這樣的:

$result = array(
    '2011-11-02' => array(
     'mobile' => 2, 
     'desktop' => 1, 
    ), 
    '2011-11-04' => array(
     'mobile' => 0, 
     'desktop' => 1, 
    ), 
    '2011-11-05' => array(
     'mobile' => 2, 
     'desktop' => 0, 
    ), 
); 
+0

我不太清楚你的意思..你能給我一個例子,指出我在正確的方向嗎? –

+0

我更新了我的答案 – LeleDumbo

+0

這是個好主意!我將在今天晚些時候測試並寫下,現在+1 –