在foreach中更改数组

时间:2017-08-31 11:47:10

标签: php arrays

我在foreach中编辑我的数组,但是当我返回值时,我只得到一个数组而不是我的500个事件。

有人可以解释为什么我不能拿到我的500阵列吗?

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 个答案:

答案 0 :(得分:1)

你必须像下面这样做(更改被评论): -

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
}