2017-11-11 74 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']; 
     } 
    } 

}