php - 在 foreach 循环之前使用 array_splice
问题描述
我正在使用php 简单的 dom
我正在尝试在 foreach 循环之前清理一个数组,到目前为止我的代码:
$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) ;
我想在循环之前摆脱数组1和 [2],我想我正在快速获取对象和数组,有什么想法吗?
解决方案
不要在发送到数组之前尝试拼接,而是在以下操作之后进行:
array_splice($links, 0,2);
print_r($links);
或取消设置:
unset($links[0], $links[1]);
print_r($links);
推荐阅读
- mysql - 安装mysql时文件丢失
- python - 如何使用 Flask-Migrate 轻松更改列?
- sql - 以纳秒为单位的时间戳列上的 Hive 窗口函数范围
- mysql - 当一条路线当前正在向数据库插入大量数据时,如何使所有其他路线正常工作?
- java - 如何在 TranslateAnimation 中获得位置?
- angular - 减少高图表中 X 轴上的数据点数量
- node.js - 错误 [ERR_PACKAGE_PATH_NOT_EXPORTED]: No "exports" main 在 /app/node_modules/@babel/helper-compilation-targets/package.json 中解析
- javascript - React 中的条件类名?
- angular - 如何在 docker env 中使用 ngnix 反向代理通信 UI 和后端应用程序
- python - Python NameError - 名称未定义