2012-11-28 66 views
0

我有以下數組填充值:在多維數組計數重複的值,並基於該

Array 
(
[0] => Array 
    (
     [event_id] => 90 
     [event_date] => 2012-11-29 
     [multi] => 1 
    ) 

[1] => Array 
    (
     [event_id] => 86 
     [event_date] => 2012-11-29 
     [multi] => 1 
    ) 

[2] => Array 
    (
     [event_id] => 52 
     [event_date] => 2012-11-30 
     [multi] => 0 
    ) 

) 

其中我正在用下面的代碼動態生成:

 $dates = array(); 
    $curr_date = array(); 
    $iteration_date = null; 

    foreach ($query as $q) { 
     $event_id = $q->ID; 

     $curr_date['event_id'] = $event_id; 
     $curr_date['event_date'] = date('Y-m-d', get_post_meta($event_id, 'event_unix_date', true)); 


     if (empty($iteration_date)) { 
      $iteration_date = $curr_date['event_date']; 
      $curr_date['multi'] = 1; 
     } elseif ($iteration_date == $curr_date['event_date']) { 
      $curr_date['multi'] = 1; 
     } else { 
      $iteration_date = $curr_date['event_date']; 
      $curr_date['multi'] = 0; 
     } 

     array_push($dates, $curr_date); 
    } 

這是基於MySQL數據庫結果。

我在試圖做的事情是: 檢查是否有相同的數組,[event_date],如果是的話,請計算多少,做一段特殊的代碼(一個函數是好的),以及取決於什麼輸出時,根據該條件將[multi]的值設置爲1,2或3。

基本邏輯是:

檢查,看是否有多個[event_date]的。如果有,請檢查所有匹配重複日期的類別。如果所有日期均爲X,則將1填入[multi],如果所有日期均爲Y,則填充2,如果日期混合爲X & Y,填充爲3.如果只有一個日期,則根據哪個類別填充1或2它是在。

我以爲我是通過循環查詢(它是由event_date DESC)在正確的軌道上,但這不會讓我檢查個別條目類別和填充基礎上。

任何人都可以指向正確的方向嗎?

+0

您能否在您的句子「基本邏輯是:」之後立即更改/提供段落的更多細節?另外,您能否提供您嘗試過的代碼片段,但這些代碼並不適合您? – RonaldBarzell

回答

0

我們是否每次假設一個有序的多維數組(通過升序[event_date])? 如果是這樣,你可以:

function multi_counter($big_array, $sorted, $storage){ 
$multi_count=0; // since by default there are no multiples 
$multi_items=array(0); // starting at the beginning of the array 
$length = count($big_array); // counting before loop 
for($i=0;$i<$length-1;$i++){ // the -1 is so we don't get undefined index on last run 
    if($big_array[$i][$sorted]===$big_array[$i+1][$sorted]){ //testing for similarity 
     $multi_items[]=$i+1;  // adding the multi found to temp storage 
     $multi_count++;    // adding to # of multis 
// CALL A FUNCTION HERE SINCE THEY MATCH??? 
    }else{ 
     foreach($multi_items as $key=>$multi){ // if not similar, go back and assign stored multis 
      $big_array[$multi][$storage]=$multi_count; // assigning stored vals 
     } 
     $multi_count=0; // resetting counter 
     $multi_items=array($i+1); // resetting to next item 
    } 
} 
foreach($multi_items as $key=>$multi){ // forcing assign at end of loop 
    $big_array[$multi][$storage]=$multi_count; 
} 
return $big_array; // returning the newly modified array 

}

難道這就是你在說什麼?你可以插入一個調用另一個函數,我在其中註釋了一個。問題不清楚,但這就是你想要的。

該函數分析給出的數組作爲第一個參數,檢查每個子數組中的第二個參數是否相似,並將它們存儲在一個名爲您爲第三個參數放置的鍵中的子數組中。

+0

我很欣賞這一點的代碼。雖然這不是我所需要的,但我能夠修改它來實現它。謝謝。 – tr3online

0

您可以跟蹤數組中的日期。然後當你循環你的事件時,將日期遞增一。在循環結束時,您會知道每個日期有多少事件。然後你需要循環回來並添加你的多記錄。你也可以使用日期數組作爲循環。

$dates = new array(); 

foreach ($query as $q) { 
    if(array_key_exists($q['event_date'], $dates) 
    $dates[$q['event_date']]++; 
    else 
    $dates[$q['event_date']] = 1; 
}