2012-11-28 4 views
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); 
    } 

이것은 :

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 
    ) 

) 

나는 다음과 같은 코드를 즉시 생성하고 MySQL 데이터베이스 결과를 기반으로합니다.

내가 무엇을 시도하고있어입니다 : 체크인 (함수가 괜찮) 같은 [event_date]와 배열이있는 경우 그렇다면, 참조, 코드의 특별한 조각을 얼마나 많은 계산하고,에 따라 어떤 이 출력되면 해당 조건에 따라 [multi] 값을 1, 2 또는 3으로 설정하십시오.

기본 논리는 다음과 같습니다 여러 [event_date]의가있는 경우

확인 확인합니다. 일치하는 중복 날짜가있는 경우 카테고리를 확인하십시오. 모든 날짜가 X 인 경우 [multi]을 1로 채우고 모든 날짜가 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; 
} 
관련 문제