產生不重複的隨機數
您目前使用 rand() 函數產生隨機 Yelp 清單的方法不能確保清單的唯一性。為了防止重複,請考慮實施更聰明的隨機化策略。
一種方法是對代表列表索引的一系列數字使用本機 shuffle() 函數。這會產生一個沒有重複項的打亂數組:
<code class="php">$numbers = range(0, 19); shuffle($numbers);</code>
另一種方法是建立一個自訂randomGen() 函數,該函數接受最小、最大和所需隨機數量的參數:
<code class="php">function randomGen($min, $max, $quantity) { $numbers = range($min, $max); shuffle($numbers); return array_slice($numbers, 0, $quantity); }</code>
要在PHP 腳本中實現此功能,請使用randomGen() 產生隨機清單ID 並將其儲存在資料庫表中。每次刷新頁面時,檢查產生的清單 ID 是否與儲存的值相符。如果沒有,則顯示該清單並使用新 ID 更新表格。
此方法可確保所有 20 個清單在發生任何重複之前都顯示一次。這是更新的程式碼片段:
<code class="php"><?php $businesses = json_decode($data); $db = new PDO('mysql:host=localhost;dbname=yelp_listings', 'root', 'password'); // Generate a random listing ID using randomGen() $listing_id = randomGen(1, 20, 1)[0]; // Check if the listing ID matches the stored value $stmt = $db->prepare('SELECT listing_id FROM shown_listings WHERE id = ?'); $stmt->execute([$listing_id]); // Display the listing if it hasn't been shown yet if ($stmt->rowCount() == 0) { $business = $businesses->businesses[$listing_id - 1]; echo "<img border="0" src="%22%20.%20%24business->image_url%20.%20%22" alt="PHP中如何保證隨機數唯一且不重複?" ><br>"; echo $business->name . "<br>"; echo "<img border="0" src="%22%20.%20%24business->rating_img_url_large%20.%20%22" alt="PHP中如何保證隨機數唯一且不重複?" ><br>"; // Add the listing ID to the shown_listings table $stmt = $db->prepare('INSERT INTO shown_listings (id) VALUES (?)'); $stmt->execute([$listing_id]); } ?></code>
以上是PHP中如何保證隨機數唯一且不重複?的詳細內容。更多資訊請關注PHP中文網其他相關文章!

ThebestapproachforsendingemailsinPHPisusingthePHPMailerlibraryduetoitsreliability,featurerichness,andeaseofuse.PHPMailersupportsSMTP,providesdetailederrorhandling,allowssendingHTMLandplaintextemails,supportsattachments,andenhancessecurity.Foroptimalu

使用依賴注入(DI)的原因是它促進了代碼的松耦合、可測試性和可維護性。 1)使用構造函數注入依賴,2)避免使用服務定位器,3)利用依賴注入容器管理依賴,4)通過注入依賴提高測試性,5)避免過度注入依賴,6)考慮DI對性能的影響。

phpperformancetuningiscialbecapeitenhancesspeedandeffice,whatevitalforwebapplications.1)cachingwithapcureduccureducesdatabaseloadprovesrovessetimes.2)優化

ThebestpracticesforsendingemailssecurelyinPHPinclude:1)UsingsecureconfigurationswithSMTPandSTARTTLSencryption,2)Validatingandsanitizinginputstopreventinjectionattacks,3)EncryptingsensitivedatawithinemailsusingOpenSSL,4)Properlyhandlingemailheaderstoa

TOOPTIMIZEPHPAPPLICITIONSFORPERSTORANCE,USECACHING,數據庫imization,opcodecaching和SererverConfiguration.1)InlumentCachingWithApcutCutoredSatfetchTimes.2)優化的atabasesbasesebasesebasesbasesbasesbaysbysbyIndexing,BeallancingAndWriteExing

依賴性注射inphpisadesignpatternthatenhancesFlexibility,可檢驗性和ManiaginabilybyByByByByByExternalDependencEctenceScoupling.itallowsforloosecoupling,EasiererTestingThroughMocking,andModularDesign,andModularDesign,butquirscarecarefulscarefullsstructoringDovairing voavoidOverOver-Inje

PHP性能優化可以通過以下步驟實現:1)在腳本頂部使用require_once或include_once減少文件加載次數;2)使用預處理語句和批處理減少數據庫查詢次數;3)配置OPcache進行opcode緩存;4)啟用並配置PHP-FPM優化進程管理;5)使用CDN分發靜態資源;6)使用Xdebug或Blackfire進行代碼性能分析;7)選擇高效的數據結構如數組;8)編寫模塊化代碼以優化執行。

opcodecachingsimplovesphperforvesphpermance bycachingCompiledCode,reducingServerLoadAndResponSetimes.1)itstorescompiledphpcodeinmemory,bypassingparsingparsingparsingandcompiling.2)useopcachebachebachebachebachebachebachebysettingparametersinphametersinphp.ini,likeememeryconmorysmorysmeryplement.33)


熱AI工具

Undresser.AI Undress
人工智慧驅動的應用程序,用於創建逼真的裸體照片

AI Clothes Remover
用於從照片中去除衣服的線上人工智慧工具。

Undress AI Tool
免費脫衣圖片

Clothoff.io
AI脫衣器

Video Face Swap
使用我們完全免費的人工智慧換臉工具,輕鬆在任何影片中換臉!

熱門文章

熱工具

DVWA
Damn Vulnerable Web App (DVWA) 是一個PHP/MySQL的Web應用程序,非常容易受到攻擊。它的主要目標是成為安全專業人員在合法環境中測試自己的技能和工具的輔助工具,幫助Web開發人員更好地理解保護網路應用程式的過程,並幫助教師/學生在課堂環境中教授/學習Web應用程式安全性。 DVWA的目標是透過簡單直接的介面練習一些最常見的Web漏洞,難度各不相同。請注意,該軟體中

mPDF
mPDF是一個PHP庫,可以從UTF-8編碼的HTML產生PDF檔案。原作者Ian Back編寫mPDF以從他的網站上「即時」輸出PDF文件,並處理不同的語言。與原始腳本如HTML2FPDF相比,它的速度較慢,並且在使用Unicode字體時產生的檔案較大,但支援CSS樣式等,並進行了大量增強。支援幾乎所有語言,包括RTL(阿拉伯語和希伯來語)和CJK(中日韓)。支援嵌套的區塊級元素(如P、DIV),

WebStorm Mac版
好用的JavaScript開發工具

VSCode Windows 64位元 下載
微軟推出的免費、功能強大的一款IDE編輯器

SublimeText3漢化版
中文版,非常好用