dqd54099 2019-01-31 17:38
浏览 182
已采纳

在foreach循环之前使用array_splice

I'm using php simple dom

I'm trying to clean up an array prior to a foreach loop, my code so far:

$html = file_get_html('test/php/refs.calendar.html');

$links = array();

foreach ($html->find('ul') as $ul) {

        $links[] = $ul->plaintext ;    

}

echo '<pre>',print_r($links, 1),'</pre>'; 


prints out:

Array
(
    [0] => bla bla
    [1] => more bla bla 
    [2] => some text
    [3] => some more text
    [4] => some more text
    [5] => some more text
)

prior to foreach loop i tried:

$links[] = array_splice($html->find('ul'), 1, 2) ;

I would like to get rid of array1 and [2] before the loop, I think i'm getting objects and arrays in a twizzle, any ideas guys?

  • 写回答

1条回答 默认 最新

  • dqc19941228 2019-01-31 18:01
    关注

    Instead of trying to splice before sending into the array, do it after:

        array_splice($links, 0,2);
        print_r($links);
    

    or unset:

        unset($links[0], $links[1]);
        print_r($links);
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 无源定位系统的时差估计误差标准差
  • ¥15 请问这个代码哪里有问题啊
  • ¥20 python--version在命令端输入结果Python is not defined怎么办?还有pip不是exe格式是不是没安装成功?
  • ¥15 通过GaussianView进行结构微调消除虚频
  • ¥15 调用transformers库
  • ¥15 由于导出的数据名字中带有/,导致Matlab打不开,怎么办?
  • ¥15 新硬盘安装的程序总是崩溃,提示遇到错误
  • ¥15 openpcdet自制数据集评估bev精度和3d精度相同
  • ¥15 excel 上下按钮 显示行
  • ¥20 云卓h12pro 数传问题