格式化php for-loop [英] Formatting php for-loop

查看:143
本文介绍了格式化php for-loop的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我是新来的PHP,我正在写一个脚本,以2014年1月23日下午11:01为形式的时间戳,并将其分解成多维数组,包括月份,日期,年,小时和分钟。这里是我的代码:

$ $ p $ $ $ $ $ $ $ $ $ $ $ $ $ $ $ raw_data = array(1993年1月20日下午10:20,1991年9月6日在6:23 PM);
var_dump($ raw_data);
$ num_dates = count($ raw_data);
$ b $ //第1步:将内容分成月,日,年,小时和分钟
($ i = 0; $ i =($ num_dates-1); $ i ++) {
$ partial_data = implode(preg_split(/ [AZ] {2} /,$ raw_data [$ i]));
$ broken_data [$ i] = preg_split(/ [\s,:] /,$ partial_data);
unset($ broken_data [$ i] [2]);
unset($ broken_data [$ i] [3]);





$ b

由于某些原因,当我在终端中运行它似乎是一个无限循环。我测试了一维数组for循环内的代码,输出是我期望的,所以我很确定我的问题是因为我不知道如何格式化for循环在PHP中。有没有人看到一个明显的初学者错误?

解决方案

因此: $ i =($ num_dates- 1)您需要检查 == 的条件,不要使用赋值 = 。虽然在这种情况下,似乎你想要做这样的事情: $ i <=($ num_dates-1)。您可能忘记输入<


So I am new to php and I am writing a script to take a time stamp in the form of "January 23, 2014 at 11:01PM" and break it into a multi-dimensional array with elements for month, date, year, hour, and minutes. Here is my code:

$raw_data= array("January 20, 1993 at 10:20PM", "September 6, 1991 at 6:23PM");
        var_dump($raw_data);
        $num_dates= count($raw_data);

//Step 1: break content into month, day, year, hour, and minutes
    for ($i=0; $i=($num_dates-1); $i++) {   
        $partial_data = implode(preg_split("/[A-Z]{2}/", $raw_data[$i]));
        $broken_data[$i] = preg_split("/[\s,:]/", $partial_data);
        unset($broken_data[$i][2]);
        unset($broken_data[$i][3]);
}

For some reason, when I run this in the terminal it seems to be an infinite loop. I tested the code inside the for-loop on a one-dimensional array, and the output was what I expected, so I am pretty sure my problem is because I don't know how to format a for loop in php. Does anyone see an obvious beginner mistake?

解决方案

Because of this: $i=($num_dates-1) You need to check the condition ==, not use an assignment =. Although in this case it appears you want to do something like this: $i <= ($num_dates-1). You may have just forgot to type <.

这篇关于格式化php for-loop的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆