2017-11-11 72 views
-2

編輯:提供答案PHP字符串插入與鍵值對數組

這裏是我的循環,將打印day of month = # of entries per day

$count = 0; 
foreach($date as $dateKey=> $dateVal) { 
    echo $dateVal."=".$count; 

     foreach($Posts as $PostKey => $PostVal){ 

     if($dateVal == $numVal) { 
      echo $dateVal."=".$numVal['count']; 
     } 
    } 

} 

將打印此

01 = 0 
02 = 0 
03 = 12 
04 = 0 
05 = 13 
06 = 0 
07 = 16 
08 = 0 

哪有用鍵值對將它推入數組中,並使其成爲這樣。

$arr[01 => 0, 02 => 0, 03 => 12 ...] 

編輯:

製造其與此

$date = count($date) 
$arr = array(); 
for($i = 0; $i < $date; $i++){ 
    $arr[$i]['date'] = $date[$i]; //insert in array 
    $arr[$i]['count'] = 0; //if no entry for e.g(day 1) insert 0 

    foreach($numbers as $PostKey => $PostVal){ 
     if($arr[$i]['date'] == date('d',strtotime($PostVal['date']))) { 
      $arr[$i]['count'] = $PostVal['count']; 
     } 
    } 
} 

工作將打印這種方式

Array ([0] => Array ([count] => 1 [date] => 2017-11-07) [1] => Array ([count] => 1 [date] => 2017-11-09) [2] => Array ([count] => 2 [date] => 2017-11-10) [3] => Array ([count] => 1 [date] => 2017-11-11)) 
+0

*「如何可以推入一個數組...」 - - 不打印值,[把它們放入一個數組](http:// php.net/manual/en/language.types.array.php#language.types.array.syntax.modifying)。 – axiac

回答

0

你可以做到這一點通過創建一個數組,並小號它的關鍵和價值如下所示:

$count = 0; 
$arr = array(); 
foreach($date as $dateKey=> $dateVal) { 
    echo $dateVal."=".$count; 

     foreach($numbers as $numKey => $numVal){ 

     if($dateVal == $numVal) { 
      $arr[$dateVal] =$numVal['count']; 
     } 
    } 

}