亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

在列出這些項目之前顯示符合特定條件的文件/項目的計數

在列出這些項目之前顯示符合特定條件的文件/項目的計數

PHP
三國紛爭 2022-06-11 18:15:02
我似乎無法在任何地方找到答案。我正在嘗試計算 FTP 文件夾中滿足特定條件的文件數。我正在使用以下內容:/* * Filename examples:  * Store Evaluation_10950_2019-12-03_6980.pdf * Store Survey_13532_2019-11-29.pdf */$file_list = ftp_nlist($ftp_connection, "."); $currentDate = date('Y-m');$countFiles = count($file_list);// Title of Completed Evaluatonsecho '<strong>'.$countFiles.' Completed Evaluations:</strong><br><br>'; foreach($file_list as $file) {//Only get Current Month PDF files    if ((strpos($file, '.pdf') !== false) && (strpos($file, 'Store Evaluation') !== false) && (strpos($file, $currentDate) !== false)) {        // Remove Store Evaluation_ from string        $strippedEvals1 = ltrim($eval, 'Store Evaluation_');        // Remove store number from string        $strippedEvals2 = strstr($strippedEvals1, '_');        // Remove _ from string        $strippedEvals3 = str_replace('_','',$strippedEvals2);        // Remove everything after the date in string        $strippedEvalDate = substr_replace($strippedEvals3 ,"", -8);         // Get just the store number        $strippedEvalStoreNum = substr($strippedEvals1, 0, strpos($strippedEvals1, "_"));         // Print store number and date        echo "<strong>".$strippedEvalStoreNum."</strong> (".$strippedEvalDate.")<br>";    }}我能夠根據我指定的條件列出所有文件;但是,現在我想數一數并說出頂部有多少。上面的代碼顯然是在沒有條件的情況下輸出了文件夾中所有文件的個數。我試圖$countFiles = count(strpos($file_list, '.pdf'));只測試一個條件,但這沒有產生任何結果。解決這個問題的正確方法是什么?
查看完整描述

1 回答

?
青春有我

TA貢獻1784條經驗 獲得超8個贊

count()不是在循環時回顯條件,而是可以將其存儲在一個數組中,然后在數組的末尾用 a 輸出它$output:


$output = array(); //create an empty array to store our output


foreach($file_list as $file) {

//Only get Current Month PDF files

    if ((strpos($file, '.pdf') !== false) && (strpos($file, 'Store Evaluation') !== false) && (strpos($file, $currentDate) !== false)) {

        // Remove Store Evaluation_ from string

        $strippedEvals1 = ltrim($eval, 'Store Evaluation_');

        // Remove store number from string

        $strippedEvals2 = strstr($strippedEvals1, '_');

        // Remove _ from string

        $strippedEvals3 = str_replace('_','',$strippedEvals2);

        // Remove everything after the date in string

        $strippedEvalDate = substr_replace($strippedEvals3 ,"", -8); 

        // Get just the store number

        $strippedEvalStoreNum = substr($strippedEvals1, 0, strpos($strippedEvals1, "_")); 

        // Print store number and date

        $output[] =  "<strong>".$strippedEvalStoreNum."</strong> (".$strippedEvalDate.")"; //add to array instead of echo

    }

}


echo '<strong>'.count($file_list).' Completed Evaluations</strong><br><br>'; 


echo '<strong>'.count($output).' Evaluations Met My Conditions:</strong><br><br>'; //echo the number of files that met the conditions


echo implode('<br/>', $output); //echo the converted file names


查看完整回答
反對 回復 2022-06-11
  • 1 回答
  • 0 關注
  • 86 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號