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

PHP数组父级没有索引,子级有索引,除非父级是子级,php,arrays,key,indexing,Php,Arrays,Key,Indexing,我的脚本获取给定的数组并将值提交到数据库。不幸的是,我有一些问题。如果使用一个$call提交数组,则没有索引,只有值。它也没有说数组。我敢肯定,这是某种简单的缺乏理解,在世界各地 if($result['calls']['call']) { foreach($result['calls']['call'] as $call) { . . 部分。下面是作为$result传回的完整数组的示例: <response method="switchvox.callL

我的脚本获取给定的数组并将值提交到数据库。不幸的是,我有一些问题。如果使用一个$call提交数组,则没有索引,只有值。它也没有说数组。我敢肯定,这是某种简单的缺乏理解,在世界各地

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

        )

)
代码:

实际的问题是,为什么我不能通过一次$call获得结果的索引

我想这张照片正是你所看到和担心的

但这不是你的问题。$result['calls']['call']数组的单个调用数据错误。您的问题在于为您创建数组的函数`print_r$call只打印它拥有的内容

上升期
您可以尝试打印\r$result['calls']['call'],而不是for循环。在这里,您应该可以看到单次调用数组的外观。

对不起,我不明白您的问题。@hakre;向下滚动第二个代码块。您将看到没有任何键的数组。我不知道他们的钥匙为什么出错。可能是SwitchvoxRequest中的错误吧?-@hakre-可能是,但我可以在调用中成功运行该函数,我知道该调用有一个超出此脚本的结果,并且我得到了完整的预期响应。它不是函数,因为我可以在$call中成功运行该函数,并得到一个正确的结果。除非我称之为tihngs的上下文中断,但我不知道如何测试它。我将更改它并在您的评论中发布更改。
<?
//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);
                                                        }
                        }
                }
        //}
}
?>
if($result['calls']['call']) {
    foreach($result['calls']['call'] as $call) {
        echo "<br>";
        print_r($call);