在循环中获取数组数据 - php

时间:2013-09-26 12:58:54

标签: php arrays function

我想创建一个函数来传递一年中几周的值和该周的开始日期。我只是将print_r($ week_array)放在while循环中时得到输出(这没用)我需要能够在循环外部获取数组。我在这里看不到发生了什么......谢谢。

function GetWeeks(){
    $year = date('Y');
    $firstDayOfYear = mktime(0, 0, 0, 1, 1, $year);
    $firstthursday = strtotime('thursday', $firstDayOfYear);
    $firstMonday = strtotime('-3 days', $firstthursday);
    $nextMonday = strtotime('monday', $firstDayOfYear);
    $nextSunday = strtotime('sunday', $firstMonday);
    $weeknumber = "1";
    $week_array = '';

    while (date('Y', $nextMonday) == $year) {
        if ($weeknumber == 53) { 
            exit; 
        }

        if ($weeknumber == 1) {
            $fromdate = date('Y-m-d', $firstMonday);
            $todate   = date('Y-m-d', $nextSunday);
            $week_array[] = array($weeknumber => $fromdate);
            date('Y-m-d', $firstMonday);
            date('Y-m-d', $nextSunday);
            $weeknumber = $weeknumber + 1;
        }

        $nextSunday = strtotime('sunday', $nextMonday);
        $fromdate = date('Y-m-d', $nextMonday);
        $todate = date('Y-m-d', $nextSunday);
        $nextMonday = strtotime('+1 week', $nextMonday);
        $nextSunday = strtotime('+1 week', $nextSunday);
        #array_push($array, $fromdate);
        $week_array[] = array($weeknumber => $fromdate);
        $weeknumber = $weeknumber + 1;
        print_r($week_array); // Print the array values for each loop.
    }

    print_r($week_array); -> Does not work at all (dont print anything)

    return array($week_array);

}

3 个答案:

答案 0 :(得分:1)

首先向数组声明:

$week_array = array();

尝试之后:

$arr =  array($weeknumber => $fromdate);
array_push($week_array, $arr);

而不是:

$week_array[] = array($weeknumber => $fromdate);

答案 1 :(得分:1)

将此行if ($weeknumber == 53) { exit; }更改为if ($weeknumber == 53) { break; }

答案 2 :(得分:0)

退出While循环。检查一下

请参阅此if ($weeknumber == 53) { exit}

<?php
function GetWeeks(){
    $year           = date('Y');
$firstDayOfYear = mktime(0, 0, 0, 1, 1, $year);
$firstthursday  = strtotime('thursday', $firstDayOfYear);
$firstMonday    = strtotime('-3 days', $firstthursday);
$nextMonday     = strtotime('monday', $firstDayOfYear);
$nextSunday     = strtotime('sunday', $firstMonday);

$weeknumber = "1";
//$week_array = '';
$week_array = array();
while (date('Y', $nextMonday) == $year) 
{

    if ($weeknumber == 1){
        $fromdate = date('Y-m-d', $firstMonday);
        $todate   = date('Y-m-d', $nextSunday);
        $week_array[] = array($weeknumber => $fromdate);
        date('Y-m-d', $firstMonday);
        date('Y-m-d', $nextSunday);
        $weeknumber = $weeknumber + 1;
    }
    $nextSunday     = strtotime('sunday', $nextMonday);
    $fromdate = date('Y-m-d', $nextMonday);
    $todate   = date('Y-m-d', $nextSunday);
    $nextMonday = strtotime('+1 week', $nextMonday);
    $nextSunday = strtotime('+1 week', $nextSunday);
    #array_push($array, $fromdate);

    $week_array[] = array($weeknumber => $fromdate);
    $weeknumber = $weeknumber + 1;

    if ($weeknumber == 53) {
    echo "<pre>";
    print_r($week_array);
    exit; 
    }
            //print_r($week_array);
             //-> Print the array values for each loop.
}
 //-> Does not work at all (dont print anything)
exit;
return array($week_array);

}
GetWeeks();
?>