0
我想優化增加這個循環的速度。我想它,如果任何人有任何想法,以提高速度因爲有成千上萬的會員,它確實需要時間的時間來完成目前PHP的foreach代碼的性能速度
SQL存儲過程返回MembersMobileByVenue這 - > 例
FirstName Phone Venue
Aaron 04******* 7272CD46D51F
Brad 04******* CF105BB0
Adam 04******* 7272CD46D51F
Craig 04******* CF105BB0
PHP
$venueIDS = isset($_POST['location']) ? $_POST['location'] : array();
$msg = $_POST['message'];
$response = array();
if(!empty($venueIDS)){
$Members = GoldCardMembers::MembersMobileByVenue();
foreach($Members as $Member){
if(in_array($Member->Venue, $venueIDS)){
$destination = $Member->Phone;
$text = 'Hi ' . $Member->FirstName . ' ' .$msg. '. Reply STOP to opt out';
$ref = 'Members';
$content = '&to='.rawurlencode($destination).
'&message='.rawurlencode($text).
'&ref='.rawurlencode($ref);
$smsbroadcast_response = sendSMS($content);
$response_lines = explode("\n", $smsbroadcast_response);
foreach($response_lines as $data_line){
$message_data = "";
$message_data = explode(':',$data_line);
if($message_data[0] == "OK"){
array_push($response, "The message to ".$message_data[1]." was successful, with reference ".$message_data[2]."\n");
}elseif($message_data[0] == "BAD"){
array_push($response, "The message to ".$message_data[1]." was NOT successful. Reason: ".$message_data[2]."\n");
}elseif($message_data[0] == "ERROR"){
array_push($response, "There was an error with this request. Reason: ".$message_data[1]."\n");
}
}
}
}
}
foreach($response as $message){
echo $message;
}
那裏有多個'foreach()'。包括可能導致問題的'if/elseif/etc'。馬上,您可以將您的'if/elseif/etc'改爲'switch/case'語句。這隻會提供邊際性能變化。你應該做的是調試和分析腳本以找到真正的瓶頸。它可能是幾件事中的任何一件,即:你的'GoldCardMembers :: MembersMobileByVenue()','sendSMS()','if/elseif',多個'foreach()'調用。我建議分塊處理數據並進行處理。 – Darren