我正在遍历一个电子表格,其中可能包含如下信息:
To From
LAX/SAF/ORL ORL/SAF
LAX/DUB DUB/SAF
SAF/LON LON/BNE/SYD我需要遍历这些信息,这样我就可以得到如下内容:
Result 1: Lax -> Orl
Result 2: Lax -> Saf
Result 3: Saf -> Orl
Result 4: Saf -> Saf
Result 5: Orl -> Orl
Result 6: Orl -> Saf这是我到目前为止所知道的:
$row = "LAX/SAF/ORL";
$row2 = "ORL/SFO";
preg_match_all('([A-Za-z]+)', $row, $matches);
foreach ($matches[0] as $location_to){
echo $location_to . " -> " . $row2 . "<br />"; //currently doesn't loop through row2
}如果不是这两组数据,我怎么做呢?我现在被困在如何..。两次这样的foreach,以获得我需要的输出。
任何帮助都将非常感谢(这需要通过一个循环,因为它是电子表格上的一行,并有其他分配给它的数据)。
这基本上是一种多次旅行的方式。
谢谢
发布于 2013-02-25 09:13:26
试试这个:
$row = "LAX/SAF/ORL";
$row2 = "ORL/SFO";
$rows = array($row, $row2);
foreach ($rows as $row) {
preg_match_all('([A-Za-z]+)', $row, $matches);
foreach ($matches[0] as $location_to){
echo $location_to . " -> " . $row2 . "<br />"; //currently doesn't loop through row2
}
}基本上,您拥有的代码将对$rows数组中的每个元素执行。
将来,如果有更多的行(即文件的每一行),只需将它们添加到$rows数组中,代码就会遍历每一行。
https://stackoverflow.com/questions/15058667
复制相似问题