doukekui0914 2011-07-25 17:13
浏览 60

PHP数组父项没有索引,子项没有,除非父项是子项

My script takes the arrays given to it and submits values to a database. Unfortunately, I am having issues with some of it. If an array is submitted with one $call, there is no index, just the values. It also does not say Array (). I am positive this is some kind of simple lack of understanding, somewhere around the

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

part. Below is an example of the full array passed back as $result:

<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>

And here is what I see for the arrays:

********************* 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>
                )

        )

)

Code:

<?
//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);
                                                        }
                        }
                }
        //}
}
?>

Actual question is why am I not getting the indexes on results with one $call?

  • 写回答

1条回答 默认 最新

  • douyao4632 2011-07-25 19:10
    关注
    if($result['calls']['call']) {
        foreach($result['calls']['call'] as $call) {
            echo "<br>";
            print_r($call);
    

    I think this print_r is what you see and are worried about

    That is not your problem, though. Your $result['calls']['call'] array has the wrong data for your single calls. Your problem is in the function that creates the array for you. `print_r($call)' only prints what it has.

    UPTDATE
    Instead of the for loop you can try and print_r($result['calls']['call']). There you should see what your single call array looks like.

    评论

报告相同问题?

悬赏问题

  • ¥15 关于#matlab#的问题:在模糊控制器中选出线路信息,在simulink中根据线路信息生成速度时间目标曲线(初速度为20m/s,15秒后减为0的速度时间图像)我想问线路信息是什么
  • ¥15 banner广告展示设置多少时间不怎么会消耗用户价值
  • ¥16 mybatis的代理对象无法通过@Autowired装填
  • ¥15 可见光定位matlab仿真
  • ¥15 arduino 四自由度机械臂
  • ¥15 wordpress 产品图片 GIF 没法显示
  • ¥15 求三国群英传pl国战时间的修改方法
  • ¥15 matlab代码代写,需写出详细代码,代价私
  • ¥15 ROS系统搭建请教(跨境电商用途)
  • ¥15 AIC3204的示例代码有吗,想用AIC3204测量血氧,找不到相关的代码。