我有下面的代码,通过一个数组步骤,并将数组元素组分成一个字符串。它应该遍历此数组15次并连接输出字符串。它仅使用输出字符串中最后一条记录而不是前一条记录的数据。
欢迎任何想法。
//Now get the data for each individual
for ($i=$ind[0];$i<$ind[14];$i++){
//echo $persons[$i]."<br>";
//get the id
if (strpos($persons[$i],"0 @")!==false){
//if (strpos($record,"0 @I")!==false){
$s = explode("@",$persons[$i]);
for($k=1;$k<=count($s)-1;$k+=2){
$data .= $s[$k]." ";
$rec_id = "P ".$s[$k]."=";
}
}
//get name
if (strpos($persons[$i],"1 NAME")!==false){
$n = str_replace('/','',$persons[$i]);
$n = str_replace('1 NAME ','',$n);
$data .= $n." ";
$nt = $n;
$pname = $n;
//continue;
}
//Get Sex
if (strpos($persons[$i],"1 SEX")!==false){
$n = str_replace('1 SEX ','',$persons[$i]);
$data .= $n." ";
$sx = trim(strtolower($n));//."(".$row.",2):";
$row++;
}
//Get Birth
if (strpos($persons[$i],"1 BIRT")!==false){
$bf = "y";//Has a birth fact
}
if ((strpos($persons[$i],"2 DATE")!==false) && $bf =="y"){
$n = str_replace('2 DATE ','',$persons[$i]);
$gotdate = "y";
$data .= "B: ".$n." ";
$b_date = "- B:".trim($n)." in ";
}
if (((strpos($persons[$i],"2 PLAC")!==false) && $bf =="y") && $gotdate =="y"){
$n = str_replace('2 PLAC ', '', $persons[$i]);
$data .= $n;
$b_place = $n;
if (empty($b_place)){$b_place =" ";};
$bf = "n";
}
//Get Death
if (strpos($persons[$i],"1 DEAT")!==false){
$df = "y";//Has a death fact
}
if ((strpos($persons[$i],"2 DATE")!==false) && $df =="y"){
$n = str_replace('2 DATE ','',$persons[$i]);
$data .= "D: ".$n." ";
if (empty($n)){$n=" ";};
$d_date = "- D:".trim($n)." in ";
}
if ((strpos($persons[$i],"2 PLAC")!==false) && $df =="y") {
$n = str_replace('2 PLAC ', '', $persons[$i]);
$data .= $n . " <BR>";
if (empty($n)){$n=" ";};
$d_place = $n;
$df = "n";
}
}
if (empty($d_date)){$d_date=" ";};
if (empty($d_place)){$d_place=" ";};
//Now construct note field
$p_note ="= ".trim($pname)." was born ".substr($b_date,4).trim($b_place);
if (($d_date)!==" "){$p_note .=" and died ".substr($d_date,4).$d_place;}
//now write this record to string
$opf .= $rec_id.$sx."(".$row.",2):".$pname."\r\n".$b_date.$b_place."\r\n".$d_date.$d_place."\r\n".$p_note;
答案 0 :(得分:3)
您提前结束for
声明......
}
} // for is ending hear, move this to end of code
if (empty($d_date)){$d_date=" ";};
如果你在指定的行上删除了}并将其放在最后 - 你应该处理所有记录,而不仅仅是最后一个记录。
答案 1 :(得分:0)
在Nigel Ren的帮助下(非常感谢)我需要一个额外的循环来处理数据。所以一开始我现在有了
for ( $j = 0; $j < 14; $j++ ) {
for ($i = $ind[$j]; $i < $ind[$j+1]; $i++) {
然后我在我最初发布的代码示例末尾的$ opf变量行之后关闭循环。