drgwsx8405 2018-06-27 06:28
浏览 99
已采纳

PHP XML数组切片和反向或反向然后数组切片

Have an xml file ... the webpage shows the most recent 50 new nodes. When you click a link 'show more' it adds a ?node= to the url which I $_get to change the slice starting point ($start_node) ... but it doesn't. The value DOES change but the slice doesn't work. Por que?!

$master = array_slice($xml_get->xpath('unit'), $start_node);
$master = array_reverse($master);

$i = 0;

foreach ($master as $arc) {

    $last_data = $arc[0]->data;
    $last_link = $arc[0]->link;

    // doing stuff with values

    $i++;
    if ($i >= 50) {
        break;
    }
}
  • 写回答

1条回答 默认 最新

  • dongluan2612 2018-06-27 06:51
    关注

    If you don't use the third parameter of array_slice() which is the number of elements to extract, you will retrieve all entries from the start node on. When you reverse this and extract the first 50, this will always be the last 50 of all of the nodes.

    So to extract from $start_node to $start_node+50...

    $master = array_slice($xml_get->xpath('unit'), $start_node, 50);
    $master = array_reverse($master);
    foreach($master as $arc) {
        $last_data  = $arc[0]->data;
        $last_link  = $arc[0]->link;
    
        // doing stuff with values
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?