While 循环生成太多数组

发布于 2024-10-09 16:25:04 字数 2049 浏览 0 评论 0原文

我需要帮助,非常需要。我现在正在做的是一个日期选择器。几乎对其进行了更新以供我自己使用。

我需要排除特定的日期范围。我找到了一个代码来执行此操作,它使用数组。我喜欢这样。

然后我需要一种方法来创建包含范围内每个日期的数组,因为我只输入了开始日期和结束日期。找到了一个。就像魅力一样。

但现在,我有一个问题。它使用相同的 $ 创建一个新数组。因此,日历注册的唯一数组是最新的数组。本质上,我需要的只是 1 个数组。

我尝试了一些方法,但似乎没有任何效果。现在已经思考这个问题有一段时间了。有什么帮助吗? <代码>

function createDateRangeArray($strDateFrom,$strDateTo) //Changes a Range of Dates to Specific Dates
    {
    $aryRange = array(); //Creates an Array

    $iDateFrom = mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo = mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo >= $iDateFrom)
        {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry

        while ($iDateFrom<$iDateTo)
            {
          $iDateFrom += 86400; // add 24 hours
          array_push($aryRange,date('Y-m-d',$iDateFrom));
            }
        }

    return $aryRange; //Returns to step 1 and adds another value into the array
    }


$d = "SELECT startdate, enddate FROM classdetails";
$f = mysql_query($d);

while ($e = mysql_fetch_array($f)) 
    {
    while (list($g, $h) = each($e)) { $$g = $h; } 
        {
        $aryDates = createDateRangeArray($startdate,$enddate);
        print_r($aryDates);
        echo "<br />";
        }
    }

<代码>
对于那些想知道的人,我确实包含了我的一些作品的参考资料,即使是经过大量修改的。

正如您所看到的,问题在于,一旦创建了一个数组,它就只是创建了一个新数组。我尝试过使用ifelse语句、empty()、isset()、increments(甚至不知道如何使用它们,只是想了很长时间并删除了它)。

那么,我在这里能做什么呢?

我总是得到的是(我只有 2 行虚拟数据):

Array ( [0] => 2010-12-16 [1] => 2010-12-17 [2] => 2010-12-18 [3] => 2010-12-19 [4] = > 2010-12-20 [5] => 2010-12-21 [6] => 2010-12-22 [7] => 2010-12-23 Array ( [0] => 2010-12-25 [1] => 2010-12-26 [2] => 2010-12-27 [3] => 2010-12-28 [4] = >2010-12-29)

问题出在循环本身。第一个实例做了它应该做的事情。从第二个实例开始,$aryDates 就被替换了。

任何帮助将不胜感激。

I need help, badly. What I am making right now is a date picker. Pretty much updating on it for my own use.

I needed to block out specific date ranges. I found a code to do this, and it utilizes arrays. I like that.

What I then needed was a way to create the array with every date within the range, because I only entered the Start Date and the End Date. Found one. Works like a charm.

But now, I have a problem. It creates a new array using the same $. So the only array that the calender registers is the newest one. Essentially, what I need is just 1 array.

I've tried a few things, but nothing seems to work. Been thinking about this for a while now. Any help?

function createDateRangeArray($strDateFrom,$strDateTo) //Changes a Range of Dates to Specific Dates
    {
    $aryRange = array(); //Creates an Array

    $iDateFrom = mktime(1,0,0,substr($strDateFrom,5,2),     substr($strDateFrom,8,2),substr($strDateFrom,0,4));
    $iDateTo = mktime(1,0,0,substr($strDateTo,5,2),     substr($strDateTo,8,2),substr($strDateTo,0,4));

    if ($iDateTo >= $iDateFrom)
        {
        array_push($aryRange,date('Y-m-d',$iDateFrom)); // first entry

        while ($iDateFrom<$iDateTo)
            {
          $iDateFrom += 86400; // add 24 hours
          array_push($aryRange,date('Y-m-d',$iDateFrom));
            }
        }

    return $aryRange; //Returns to step 1 and adds another value into the array
    }


$d = "SELECT startdate, enddate FROM classdetails";
$f = mysql_query($d);

while ($e = mysql_fetch_array($f)) 
    {
    while (list($g, $h) = each($e)) { $g = $h; } 
        {
        $aryDates = createDateRangeArray($startdate,$enddate);
        print_r($aryDates);
        echo "<br />";
        }
    }


And for those wondering, I do include references of where some of my work is taken from, even if heavily modified.

As you can see, the problem lies with the fact that once it creates an array, it just creates a new one. I've tried using ifelse statements, empty(), isset(), increments (didn't even know how to use them, just thought a long time and deleted it).

So, what can I do here?

What I always get back is (I only have 2 rows of dummy data):


Array ( [0] => 2010-12-16 [1] => 2010-12-17 [2] => 2010-12-18 [3] => 2010-12-19 [4] => 2010-12-20 [5] => 2010-12-21 [6] => 2010-12-22 [7] => 2010-12-23 )
Array ( [0] => 2010-12-25 [1] => 2010-12-26 [2] => 2010-12-27 [3] => 2010-12-28 [4] => 2010-12-29 )

The problem lies with the loop itself. The first instance does what it is tole to do. The second instance onwards, $aryDates just gets replaced.

Any help would be greatly appreciated.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

死开点丶别碍眼 2024-10-16 16:25:04
while (list($g, $h) = each($e)) { $g = $h; } 

哦天哪。不要使用变量。特别是当你已经通过 mysql fetch 调用生成了一个完美的数组时。这就是您在调试代码时遇到问题的原因。

您可以像这样重写 while 循环,

while ($e = mysql_fetch_array($f)) {
    $aryDates = createDateRangeArray($e['startdate'],$e['enddate']);
    print_r($aryDates);
    echo "<br />";
}

可读性更高,更容易理解正在发生的事情,并且绝对不需要任何变量。

你的苦行是用湿面条鞭打 500 下,同时重复以下咒语:“我不会使用变量”

除此之外,当然它每次都会创建一个新数组,你在 createDateRange 函数的顶部告诉它:

$aryRange = array(); //Creates an Array

如果您想在多个函数调用中重用同一个数组,请将其设为静态变量:

static $aryRange = array();

这将在连续调用中保留它,然后您可以附加每个调用的日期。

while (list($g, $h) = each($e)) { $g = $h; } 

Oh good god. Do NOT use variable variables. Especially when you've got a perfectly good array being generated by the mysql fetch call as is. THis is why you're having trouble debugging your code.

You could rewrite the while loop like this

while ($e = mysql_fetch_array($f)) {
    $aryDates = createDateRangeArray($e['startdate'],$e['enddate']);
    print_r($aryDates);
    echo "<br />";
}

Far more readable, far easier to follow what's happening, and absolutely NO NEED WHATSOEVER for variable variables.

You penance is 500 lashes with a wet noodle while repeating the following mantra: "I shall not use variable variables"

Beyond that, of course it's creating a new array each time, you're telling it to at the top of your createDateRange function:

$aryRange = array(); //Creates an Array

If you want to reuse the same array over multiple function calls, make it a static variable:

static $aryRange = array();

which will preserve it across successive calls, which would then let you append each calls's dates.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文