PHP数组中的Foreach循环“分组依据”键

时间:2016-03-31 18:44:24

标签: php arrays foreach html-table

我非常接近于解决这个问题,这是使用Wordpress和Gravity Forms插件:

<?php
        $form_id = '1'; // The registration form's ID
        $entries =  GFAPI::get_entries( $form_id ); // Get all entries

        $html = "<table class='ent'>"; // Create an HTML table          
        $html .= "<tr><th>Name</th><th>Organization</th><th>Event</th><th>Date</th><th>Number Attending</th></tr>";

        $list = array(); // Array to store the original fields  
        $used = array(); // Array to stored the "used" fields

        // Loop through array of entries,each element 
        foreach($entries as $entry){

            // Get datepart from datetime of event and today's date
            $date = date_format(new DateTime($entry['8']), 'Y-m-d');
            $today = date("Y-m-d");

            // Compare event's date with current date
            if ( $date >= $today ) {

                $list[] = $entry;

                if (!empty($used[$entry['6']])) // If used[event name] not null then sum associated numattend
                {
                    $used[$entry['6']] += $entry['5'];
                }   
                else // return associated numattend
                {
                    $used[$entry['6']] = $entry['5'];
                }
            } 
        }   
        unset($entry);

       foreach ($used as $key => $value) {
          foreach ($list as $key1 => $value1) {
              $html .= "<tr><td>" . $value1['1'] . " " . $value1['2'] . "</td><td>" . $value1['93']  . "</td><td>" . $value1['6'] . "</td><td>" . $value1['8']  . "</td><td>" . $value1['5'] . "</td></tr>";
          }
          $html .= "<tr class='ent_sum_rw'><td>" . "&nbsp;" . "</td><td>" . "&nbsp;" . "</td><td>" . "&nbsp;" . "</td><td class='ent_sum'>" . "Total Attending:" . "</td><td>" . $value . "</td></tr>";
     }
     unset($value);

     $html .= "</table>";
     return $html;
?>

结果:Result1

我希望循环结束相应“事件名称”的条目列表,类似“测试事件22”不应出现在顶部条目列表中。

1 个答案:

答案 0 :(得分:2)

诀窍是填充满足您需求的Array,然后将其转换为html。我还没有对代码进行测试,但应该清楚它是如何工作的。

$aEventList = Array();
foreach($entries as $entry){
    if (!isset($aEventList[$entry['7']])){ //7 is id of event
        //if this event is not made, create it and add name and the date, enter the right numeral
        $aEventList[$entry['7']]['name'] = $entry['6'];
        $aEventList[$entry['7']]['date'] = $entry['90'];
        $aEventList[$entry['7']]['users'] = Array();
    } 
    $aEventList[$entry['7']]['users'][] = Array('name' => $entry['1'] . ' '. $entry['2'],
                                                'org' => $entry['93'],
                                                'num' => $entry['5']);
}
//now you've populated every event and you can loop through it on your desired way:
foreach ($aEventList as $iEvent => $aEvent){
    echo $aEvent['name']; //event name
    echo $aEvent['date']; // event date
    foreach ($aEvent['users'] as $aEventUser){
        echo $aEventUser['name']; // user name
        echo $aEventUser['org']; // user organisation
        echo $aEventUser['num']; // num attendees
    }
}