【问题标题】:Foreach loop "Group By" Key in PHP ArrayPHP数组中的Foreach循环“Group By”键
【发布时间】:2016-07-20 08:52:22
【问题描述】:

我非常接近解决这个问题,这是使用 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;
?>

结果:

我希望循环结束相应“事件名称”上的条目列表,例如“测试事件 22”不应显示在顶部条目列表中。

【问题讨论】:

  • 你能给我们举一个foreach($entries as $entry){ print_r($entry); }给出的例子吗?我认为您希望对每个事件进行分组,但我想先查看结果。

标签: php arrays foreach html-table


【解决方案1】:

诀窍是填充一个满足您需求的数组,然后将其转换为 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
    }
}

【讨论】:

  • 是的,这就行了!谢谢,我之前尝试过这样的事情,但是当我引用 ['key'] 显然引用错误时,它会给我错误。
  • 非常感谢 Garytje,您为我节省了很多时间!这就像一个魅力!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2011-06-09
  • 2020-05-17
  • 1970-01-01
  • 2010-12-22
  • 2014-09-19
  • 1970-01-01
  • 2018-02-09
相关资源
最近更新 更多