2011-07-25 76 views
0

我的腳本需要給它的數組和值提交到數據庫。不幸的是,我遇到了一些問題。如果一個數組被提交了一個$ call,那麼沒有索引,只是值。它也不會說Array()。我肯定這是某種簡單的不理解,在某處某處PHP數組父沒有索引,孩子做,除非家長是孩子

if($result['calls']['call']) { 
       foreach($result['calls']['call'] as $call) { 
. 
. 

部分。以下是完整陣列的一個例子傳遞迴爲$結果:

<response method="switchvox.callLogs.search"> 
    <result> 
     <calls page_number="1" total_pages="3" items_per_page="50" total_items="145"> 
      <call id="24406" origination="outgoing" start_time="2011-07-25 07:50:21" from="User Name &lt;4491&gt;" from_account_id="1118" from_name="User Name" from_number="4491" to="Voicemail Access &lt;899&gt;" to_account_id="3" to_name="Voicemail Access" to_number="899" total_duration="3" talk_duration="0"> 
       <events> 
        <event start_time="2011-07-25 07:50:21" type="OUTGOING" display="Dialed number (899)" /> 
        <event start_time="2011-07-25 07:50:21" type="INTERNAL" display="Rang Voicemail Access &lt;899&gt;" /> 
        <event start_time="2011-07-25 07:50:24" type="HANGUP" display="Call was hung up by User Name &lt;4491&gt;" /> 
       </events> 
      </call> 
      <call id="24405" origination="outgoing" start_time="2011-07-25 07:50:26" from="User Name &lt;4491&gt;" from_account_id="1118" from_name="User Name" from_number="4491" to="Voicemail Access &lt;899&gt;" to_account_id="3" to_name="Voicemail Access" to_number="899" total_duration="2" talk_duration="0"> 
       <events> 
        <event start_time="2011-07-25 07:50:26" type="OUTGOING" display="Dialed number (899)" /> 
        <event start_time="2011-07-25 07:50:26" type="INTERNAL" display="Rang Voicemail Access &lt;899&gt;" /> 
        <event start_time="2011-07-25 07:50:29" type="HANGUP" display="Call was hung up by User Name &lt;4491&gt;" /> 
       </events> 
      </call> 
     </calls> 
    </result> 
</response> 

這裏是我看到的數組:

********************* Response with more than one $call ****************************** 
2 - 1105 


Array 
(
    [id] => 24110 
    [origination] => outgoing 
    [start_time] => 2011-07-22 08:03:38 
    [from] => User Name <6520> 
    [from_account_id] => 1105 
    [from_name] => User Name 
    [from_number] => 6520 
    [to] => Voicemail Access <899> 
    [to_account_id] => 3 
    [to_name] => Voicemail Access 
    [to_number] => 899 
    [total_duration] => 35 
    [talk_duration] => 0 
    [events] => Array 
     (
      [event] => Array 
       (
        [0] => Array 
         (
          [start_time] => 2011-07-22 08:03:38 
          [type] => OUTGOING 
          [display] => Dialed number (899) 
         ) 

        [1] => Array 
         (
          [start_time] => 2011-07-22 08:03:38 
          [type] => INTERNAL 
          [display] => Rang Voicemail Access <899> 
         ) 

        [2] => Array 
         (
          [start_time] => 2011-07-22 08:04:14 
          [type] => HANGUP 
          [display] => Call was hung up by User Name <6520> 
         ) 

       ) 

     ) 

) 

Array 
(
    [id] => 24109 
    [origination] => incoming 
    [start_time] => 2011-07-22 07:50:26 
    [from] => MINNEAPOLS MN <6125551234> 
    [from_name] => MINNEAPOLS MN 
    [from_number] => 6125551234 
    [to] => User Name <6520> 
    [to_account_id] => 1105 
    [to_name] => User Name 
    [to_number] => 6520 
    [total_duration] => 122 
    [talk_duration] => 118 
    [events] => Array 
     (
      [event] => Array 
       (
        [0] => Array 
         (
          [start_time] => 2011-07-22 07:50:26 
          [type] => INCOMING_PROVIDER 
          [display] => Received call over Channel Group (VCG_B) 
         ) 

        [1] => Array 
         (
          [start_time] => 2011-07-22 07:50:26 
          [type] => INCOMING 
          [display] => Received call over channel 1 over phone number (9525551234) 
          [incoming_did] => 9525551234 
         ) 

        [2] => Array 
         (
          [start_time] => 2011-07-22 07:50:26 
          [type] => INTERNAL 
          [display] => Rang User Name <6522> 
         ) 

        [3] => Array 
         (
          [start_time] => 2011-07-22 07:50:31 
          [type] => TALKING 
          [display] => Talked to User Name <6522> for 1 minute, 10 seconds 
         ) 

        [4] => Array 
         (
          [start_time] => 2011-07-22 07:51:41 
          [type] => ASSISTED_TRANSFEREE 
          [display] => User Name <6522> transferred MINNEAPOLS MN <6125551234> to User Name <6520> 
         ) 

        [5] => Array 
         (
          [start_time] => 2011-07-22 07:51:41 
          [type] => TALKING 
          [display] => Talked to User Name <6520> for 48 seconds 
         ) 

        [6] => Array 
         (
          [start_time] => 2011-07-22 07:52:16 
          [type] => VOICEMAIL 
          [display] => Call was sent to voicemail box of User Name <6520> 
         ) 

        [7] => Array 
         (
          [start_time] => 2011-07-22 07:52:29 
          [type] => HANGUP 
          [display] => Call was hung up by MINNEAPOLS MN <6125551234> 
         ) 

       ) 

     ) 

) 
0 - 1108 

************ Below this is where the indexes go missing, and there is only one $call ************ 
1 - 1114 

24113 
outgoing 
2011-07-22 08:17:12 
User Name <4476> 
1114 
User Name 
4476 
15155551234 
15155551234 
35 
10 
Array 
(
    [event] => Array 
     (
      [0] => Array 
       (
        [start_time] => 2011-07-22 08:17:12 
        [type] => OUTGOING 
        [display] => Dialed number (15155551234) 
       ) 

      [1] => Array 
       (
        [start_time] => 2011-07-22 08:17:12 
        [type] => PROVIDER 
        [display] => Sent call over Channel Group (VCG_B) with number 15155551234 
       ) 

      [2] => Array 
       (
        [start_time] => 2011-07-22 08:17:36 
        [type] => TALKING 
        [display] => Talked to Channel Group (VCG_B) for 10 seconds 
       ) 

      [3] => Array 
       (
        [start_time] => 2011-07-22 08:17:47 
        [type] => HANGUP 
        [display] => Call was hung up by User Name <4476> 
       ) 

     ) 

) 

代碼:

<? 
//include switchvox libraries 
require_once("SwitchvoxRequest.php"); 

//define sql connection stuff 
$db_host = "host"; 
$db_user = "user"; 
$db_pass = "secret"; 
$db = "db"; 
$table_sr = "tblSalesReps"; 
$table_cd = "tblCallsMadeReceived_raw"; 
$link = mssql_connect($db_host, $db_user, $db_pass); 

//make sure we can connect 
if (!$link || !mssql_select_db($db, $link)) { 
     die('Unable to connect or select database!'); 
     } 

//define pbx connection stuff 
$sv_host = "url"; 
$sv_user = "user"; 
$sv_pass = "secret"; 

//query the salesrep table to find the account IDs available 
$acid_sql = "SELECT * FROM $table_sr WHERE [pbx_accountid] > 0"; 
$acid_res = mssql_query($acid_sql); 

//get and format the time and date as YYYY-MM-DD, format the time as HH:MM:SS 
$date = date('Y') . "-" . date('m') . "-" . date('d'); 
$time = date('H') . ":" . date('i') . ":" . date('s'); 

//take only the last hour of results, rather than an entire day 
$st_time = date('H')-2 . ":" . date('i') . ":" . date('s'); 
$st_date = date('Y') . "-" . date('m') . "-" . date('d'); 


echo "<pre>"; 

while ($row = mssql_fetch_array($acid_res, MSSQL_ASSOC)) { 
     $req = new SwitchvoxRequest($sv_host, $sv_user, $sv_pass); 
     $reqpar = array 
       (
       'account_ids' => array 
         (
         'account_id' => array 
           (
           $row['pbx_accountid'] 
           ) 
         ), 
       'start_date' => array 
         (
         $date . " " . $st_time 
         ), 
       'end_date' => array 
         (
         $date . " " . $time 
         ), 
       'sort_field' => array 
         (
         ), 
       'sort_order' => array 
         (
         'DESC' 
         ) 
       ); 

     $res = $req -> send("switchvox.callLogs.search", $reqpar); 
     $result = $res->getResult(); 
     $calls = $result['calls']['total_items']; 

     //if($calls != 0) { 
     print_r($calls); 
     echo " - "; 
     print_r($row['pbx_accountid']); 
     echo "<br><br>"; 
     //if(isset($result['calls']['call'])) { 
     if($result['calls']['call']) { 
       foreach($result['calls']['call'] as $call) { 
         echo "<br>"; 
         print_r($call); 
         $id = $call['id']; 
         //check to see if the call has already been logged 
         $id_sql = "SELECT * FROM $table_cd WHERE callID='$id'"; 
         $id_res = mssql_query($id_sql); 
         $exid = mssql_fetch_array($id_res, MSSQL_ASSOC); 

         if($exid['callID']) { 
           //print_r($exid); //uncomment to show duplicate results 
           } elseif (!$exid['callID']) { 
             //print_r($call); //uncomment to show new results 
             //varialbes to insert 
             $from = $call['from_number']; 
             $to = $call['to_number']; 
             $durat = $call['talk_duration']; 
             $start = $call['start_time']; 
             $callid = $call['id']; 
             $calltype = $call['origination']; 
             //set the proper values into extension/phonenumber 
             if($calltype == "outgoing") { 
               $extension = $from; 
               $phonenumber = $to; 
               } else { 
                 $extension = $to; 
                 $phonenumber = $from; 
               } 
             //insert the data into the table 
             $fi_sql = "INSERT INTO $table_cd (extension, phonenumber, calldatetime, duration, callID, calltype) VALUES ($extension, $phonenumber, '$start', '$durat', '$callid', '$calltype')"; 
             $fi_res = mssql_query($fi_sql); 
                 } 
         } 
       } 
     //} 
} 
?> 

實際問題爲什麼我沒有通過$ call獲得結果的索引?

+0

對不起,我只是不明白你的問題。 – hakre

+0

@hakre;在第二個代碼塊上向下滾動。您將看到沒有任何鍵的陣列。我不知道爲什麼他們的鑰匙是mising。 – lorsungcu

+0

可能是'SwitchvoxRequest'中的一個bug? - http://developers.digium.com/switchvox/?pageView=phpLibrary – hakre

回答

0
if($result['calls']['call']) { 
    foreach($result['calls']['call'] as $call) { 
     echo "<br>"; 
     print_r($call); 

我覺得這print_r的就是你看到的和擔心

那不是你的問題,但。您的$result['calls']['call']陣列對於您的單個呼叫有錯誤的數據。你的問題在於爲你創建數組的函數。 `print_r($ call)'只打印它的內容。

UPTDATE
取而代之的for循環,你可以嘗試和print_r($result['calls']['call'])。你應該看看你的單個調用數組是什麼樣的。

+0

這不是函數,因爲我可以在一個$調用中以一個結果成功運行它,並獲得正確的結果。除非我在斷斷續續的情況下,但我不知道如何測試。 – lorsungcu

+0

@lorsungcu查看更新 –

+0

我會改變它,並在您的評論中發佈更改。 – lorsungcu