Родительский массив PHP не имеет индекса, дети делают, если родитель не является дочерним - PullRequest
0 голосов
/ 25 июля 2011

Мой сценарий берет предоставленные ему массивы и передает значения в базу данных. К сожалению, у меня есть проблемы с некоторыми из них. Если массив передается с одним вызовом $, индекс отсутствует, только значения. Это также не говорит Array (). Я уверен, что это какое-то простое непонимание, где-то около

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

часть. Ниже приведен пример полного массива, переданного как $ 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>

А вот что я вижу для массивов:

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

Актуальный вопрос: почему я не получаю индексы результатов одним вызовом?

1 Ответ

0 голосов
/ 25 июля 2011
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']).Там вы должны увидеть, как выглядит ваш единственный массив вызовов.

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...