0
 <?php
            $xml = simplexml_load_string( $response->ApiCallDeatilDataFeedResult );

            foreach ( $xml->call_data as $data )
            {
            ?>
            <tr>

                <td  class="sss"><?php 

                echo $data->call_time;?></td>
                <td  class="sss"><?php 

                $init = $data->call_duration;
$hours = floor($init / 3600);
$minutes = floor(($init / 60) % 60);
$seconds = $init % 60;

echo "$minutes Min : $seconds Sec";
?></td>
                <td  class="sss"><?php echo $data->call_status;?></td>
                <td  class="sss"><?php echo $data->caller_number;?></td>
                <td  class="sss"><?php echo $data->caller_name;?></td>

                <td  class="sss"><?php echo $data->caller_city;?></td>
                <td  class="sss"><?php echo $data->caller_state;?></td>
                <td  class="sss"><?php echo $data->caller_zip;?></td>

            </tr>
            <?php
            }
            ?>
        </table>

在请求 xml 时,我收到了 xml 响应,它以升序给出结果。我需要按降序排列的 XML 响应,并且总共不需要行。

4

4 回答 4

1

@Yogesh Suthar的答案是正确的(尽管直接在 $xml 上调用 count 而不是 $xml->call_data),但我认为索引是错误的,因为数组索引从零开始并以长度 1 结束。

编辑:正如@hakra在评论中所说, call_data 不是一个数组,而是一个可迭代的 SimpleXMLElement。但是为了争论,让我们说它是。所以,我认为应该是:

for($i = count($xml->call_data) - 1 ;$i >= 0 ; $i--)

或者,如果您想避免使用索引的麻烦,请尝试使用array_reverse如果 call_data 确实是一个数组

foreach ( array_reverse($xml->call_data) as $data )
于 2012-09-28T11:07:26.237 回答
1

使用krsort进行排序。是的,使用 count() 来计算数组。

于 2012-09-28T11:08:35.210 回答
1

Simplexml 为 提供了一个迭代器foreach,如果你想反转它,你可以将它转换为一个数组,反转数组,然后foreach在数组上:

$datas = $xml->call_data;
$datas = iterator_to_array($datas, 0);
$datas = array_reverse($datas);

foreach($datas as $data)
{
    ...
}

$count = count($datas);
于 2012-09-28T11:23:40.587 回答
0

用于count行数并反向使用for循环用于降序,如本例

for($i = count($xml) ;$i >0 ; $i--)
{
   // your code
}
于 2012-09-28T11:00:44.927 回答