Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
317 views
in Technique[技术] by (71.8m points)

Formatting php for-loop

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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 <.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...