dousongxuan7507 2017-08-31 11:47
浏览 30
已采纳

在foreach中更改数组

I'm editing my array in a foreach, but when I return the values I get only one array as the result and not my 500 events.

Can someone explain why I can't get my 500 array's?

private function getEventsScroll($param = null)
{


$sql = $dbh->prepare('SELECT * FROM events WHERE date >= NOW() AND status = "0" ORDER BY `date` ASC');

$sql->execute();
$events = $sql->fetchAll(PDO::FETCH_ASSOC);

foreach ($events as &$event) {

    $output = [
        'name' => setHalveText(ucfirst($event['name']), 17),
        'sub_text' => setHalveText($event['sub_text'], 45),
        'datetime' => getDaytimeFormat($event['date'], 'Y-m-d\TH:i:s.u'),
        'dayNumber' => getDaytimeFormat($event['date'], 'd'),
        'monthName' => getDaytimeFormat($event['date'], 'F'),
        'year' => getDaytimeFormat($event['date'], 'l'),
        'time_start' => setTime($event['time_start']),
        'time_end' => setTime($event['time_start']),
        'url' => '../../event/' . setUuid($event['uid']) . ':'. getEventSlug($event['name']), 
    ];
    unset($event['name']);
    unset($event['sub_text']);
    unset($event['time_start']);
    unset($event['time_end']);

    $event = array_merge($event, $output);

    return $event;
    }
}
  • 写回答

1条回答 默认 最新

  • dpi9530 2017-08-31 11:58
    关注

    You have to do it like below (changes are commented):-

    private function getEventsScroll($param = null){
    
        $sql = $dbh->prepare('SELECT * FROM events WHERE date >= NOW() AND status = "0" ORDER BY `date` ASC');
    
        $sql->execute();
        $events = $sql->fetchAll(PDO::FETCH_ASSOC);
    
        foreach ($events as &$event) {
    
            $output = [
                'name' => setHalveText(ucfirst($event['name']), 17),
                'sub_text' => setHalveText($event['sub_text'], 45),
                'datetime' => getDaytimeFormat($event['date'], 'Y-m-d\TH:i:s.u'),
                'dayNumber' => getDaytimeFormat($event['date'], 'd'),
                'monthName' => getDaytimeFormat($event['date'], 'F'),
                'year' => getDaytimeFormat($event['date'], 'l'),
                'time_start' => setTime($event['time_start']),
                'time_end' => setTime($event['time_start']),
                'url' => '../../event/' . setUuid($event['uid']) . ':'. getEventSlug($event['name']), 
            ];
            unset($event['name']);
            unset($event['sub_text']);
            unset($event['time_start']);
            unset($event['time_end']);
    
            array_merge($event, $output);
    
        }
    
        return $events; //return at last
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥30 STK matlab python仿真
  • ¥15 关于IMageEnView 图标定位问题
  • ¥20 求解答(matlab)
  • ¥30 ffmpeg库使用过程中遇到的问题
  • ¥15 pyqt5 中python如何通过Qtwebchannel主动发消息给web前端
  • ¥15 关于HTML中title获取xml内容的问题
  • ¥15 fanuc机器人PRIO083数字信号未复原错误,如何解决?
  • ¥20 如何为现有电路板增加远程控制功能
  • ¥15 UE5打包失败,求解决
  • ¥15 请问STM32G431的CANOPEN协议函数怎么写