The application of PHP multi-dimensional array----server load Balancing

Source: Internet
Author: User

Array_multisort in PHP This function implements the sorting function of multidimensional arrays, in the call center or the billing server, perhaps we need to implement multiple servers in the weight (percentage) mode for voice distribution, then this function will help you to achieve this requirement.

The core_callback_setting in the instance will save the configuration information of the server that can be used:

ID---Field ID and the ID tag of the server

, Ast_ob_servername---The name of the server,

, ast_ob_host-----Server IP Address

, ast_ob_port-----AMI Server Port

, Ast_ob_user-----The AMI server to verify the user name

, ast_ob_pwd-------AMI Server Authentication password

, percent_rate-----Server weights

, current_threads----server is currently concurrent

, max_threads-------Server maximum concurrency

, Last_dispatch_time---The last time it was distributed to the server

, ast_version-------Soft-swap version


The following will be the concrete implementation of the logical function

Function get cb serverfunction get_callback_srv () {  global  $conn;    $CB _srv_ary=array ();   $srv _tmp_ary1=array ();   $srv _tmp_ary2=array ();      $sql = "Select id,ast_ob_servername,ast_ob_host,ast_ob_port,ast_ob_user,ast_ob_pwd,percent_rate , Current_threads,max_threads,last_dispatch_time,ast_version from core_callback_setting where  state and percent_rate ";   #echo   $sql," \ n ";   $rslt =mysql_query ($sql, $ conn);   $rcnt =mysql_num_rows ($RSLT);   if ($rcnt >0)     {       if ($rcnt  ==1)          {             #echo   "There is only one server that meets the requirements ... \ n";             $CB _srv_ary=mysql_fetch_assoc ($rslt);         &nbSP;}       else      {        for ($j =0; $j < $rcnt; $j + +)          {            $row =mysql_fetch_assoc ($rslt);            $srv _id= $row [' ID '];          $ srv_tmp_ary1[$srv _id]= $row;           $r _rate=round ($row [' Current_threads ']/$row [' percent_rate '],4);           $srv _tmp _ary2[]=array (' srv_id ' =>  $row [' ID '],                               ' Last_dispatch_time '  =>  $row [' Last_dispatch_time '],                               ' R_ Rate ' =>  $r _rate);         }          # #获取记录后, sort the records          foreach ($srv _tmp_ ary2 as  $key  =>  $row)               {                $val 1[$ key]= $row [' R_rate '];                $val 2[$key]= $row [' Last_dispatch_time '];              }          # #数组排序            array_multisort ($val 1,sort_asc, $val 2,sort_asc, $srv _tmp_ary2);            #print_r ($srv _tmp_ary2);           # #获取第一个            #echo   >>>>>>>> >>>>>>>>>>>>>>>>\n ";            $srv _id= $srv _tmp_ary2[0][' srv_id '];            #print_r ($srv _tmp_ary1[$srv _id]);           $CB _srv_ary = $srv _tmp_ary1[$srv _id];                 }          }    else     {      writelog ($sql);    }     return  $CB _srv_ary;}


This article from "Asterisk System column" blog, declined reprint!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.