需要按降序和总计数进行XML响应

时间:2012-09-28 10:51:33

标签: php xml

 <?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 个答案:

答案 0 :(得分:1)

@Yogesh Suthar的答案是正确的(尽管直接调用$ xml而不是$ xml-&gt; 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 )

答案 1 :(得分:1)

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

答案 2 :(得分: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);

答案 3 :(得分:0)

使用count表示行数,并使用for循环反向按降序排列,如下例所示

for($i = count($xml) ;$i >0 ; $i--)
{
   // your code
}