我似乎做了一些愚蠢的事情,但我看不到树木的木头。
i具有下面的代码,该代码虽然数组,并将数组元素组分为字符串。它应该遍历此数组15次,并加入输出字符串。它仅使用上一个记录中的数据,而不是输出字符串中的前14个。
任何最欢迎的想法。
//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."rn".$b_date.$b_place."rn".$d_date.$d_place."rn".$p_note;
您正在提早结束for
语句...
}
} // for is ending hear, move this to end of code
if (empty($d_date)){$d_date=" ";};
如果您在指示的行上删除此}并将其放在末端 - 您应该处理所有记录,而不仅仅是最后一个记录。
在Nigel Ren的帮助下(非常感谢),我需要一个额外的循环来处理数据。所以一开始我有
for ( $j = 0; $j < 14; $j++ ) {
for ($i = $ind[$j]; $i < $ind[$j+1]; $i++) {
,然后在我最初发布的代码示例末尾的$ opf变量线之后关闭循环。