我正在努力在 foreach 语句中输出公司列表。为了比较每个公司的第一个字母,我将替换每个公司的第一个字符。这些需要通过 if 比较器操作发送,以查看它是否是每个初始字符范围的第一次出现。var_dump 显示 srt_cmp 已找到预期的值。当我在多个组合的 if 比较中使用该值时,if 语句将不会执行。
<?php
$stringCompanyListing = '<!-- beginning of business listing -->'."\n";
$countCompanies=0;
$haveStrungAD = "";
$haveStrungEI = "";
$haveStrungJO = "";
$haveStrungPU = "";
$haveStrungVZ = "";
foreach ($files as $file){
$company = new SimpleXMLElement($file, 0, true);
$COMPANYKEY[$countCompanies] = basename($file, '.xml');
if ($countCompanies >= 1){
$currentCompany = substr($COMPANYKEY[$countCompanies],0, 1);
$previousCompany = substr(($COMPANYKEY[($countCompanies-1)]),0, 1);
$checkForNavigation = strcmp($previousCompany, $currentCompany);
// var_dump at this point show intended values such as "A" and "-1"
if ($haveStrungAD == ""){
if ($currentCompany == range("A", "D")){
if ($checkForNavigation <= -1){
$stringCompanyListing .= ' <div class="categoryHeading"><a name="atod"></a>A-D</div>';
$haveStrungAD = "done";
}
}
}
if ($haveStrungEI == ""){
if ($currentCompany == range("E", "I")){
if ($checkForNavigation <= -1){
$stringCompanyListing .= ' <div class="categoryHeading"><a name="etoi"></a>E-I</div>';
$haveStrungEI = "done";
}
}
}
// more if comparisons omitted
}
$countCompanies++;
}