2017-08-31 64 views
-3

我正在編輯我的數組在foreach中,但是當我返回值時,我只得到一個數組作爲結果而不是我的500個事件。更改數組在foreach

有人可以解釋爲什麼我不能得到我的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; 
    } 
} 
+2

刪除此行'返回$事件;'和改變這種'的foreach($事件$事件){'到'foreach($ events as&$ event){'然後再試一次 –

+1

因爲你不明白'return'的含義。 –

+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 
}