dongxingqiu7943
dongxingqiu7943
2015-10-23 08:16

更改数组中的值

  • foreach
  • loops
  • php
  • for-loop
  • arrays
已采纳

I want Year 2015,January to show 24 and February to show 25. Next year 2016 January starts at 36.

enter image description here

This is my code

<?php 
     for ($i = 0; $i <= 12; $i++) {          
         $years[$y]  = $y = date("Y", strtotime("+$i years"));
         $months[$m] = $m = date("F", strtotime("Jan +$i months"));
        foreach ($years as $year => $value[$m]){                 
             $array[$year] = $value; 

         }
     } 
     echo '<pre>'; 
     print_r($array);
     echo '</pre>';                                      
  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享
  • 邀请回答

1条回答

  • dongping5230 dongping5230 6年前

    So going from the comments, I can't really work out how you came to the code you've provided. To be honest, I would recommend looking through some PHP documentation and tutorials before continuing as you seem to be missing some knowledge of basics such as arrays and loops.

    That being said, again going by your comments, the following should produce what you're after:

    <?php
    $iterator = 24;
    $array = array();
    
    foreach(range(2015, 2016) as $year) {
        foreach(range(1, 12) as $month) {
            $dateObj   = DateTime::createFromFormat('!m', $month);
            $monthName = $dateObj->format('F');
            $array[$year][$monthName] = $iterator++;
        }
    }
    
    
    echo '<pre>'; 
    print_r($array);
    echo '</pre>';
    

    This should be easily editable to your needs.

    点赞 评论 复制链接分享