[英]First element does not have number and the number does not reset in new category
I tried to crawl this page: http://hea.uum.edu.my/index.php/academic/current-student/convocation 我尝试抓取此页面: http : //hea.uum.edu.my/index.php/academic/current-student/convocation
Here is my code 这是我的代码
<?php
require_once 'vendor/autoload.php';
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://hea.uum.edu.my/index.php/academic/current-student/convocation');
$step = array();
$i = 0;
$crawler->filter('.sppb-addon.sppb-addon-accordion')->each(function ($node) {
global $step, $i;
$step[$i]['item'] = array();
$node->filter('.sppb-addon-title')->each(function ($node) {
global $step, $i;
$step[$i]['cat'] = $node->html();
});
$j = 0;
$node->filter('.sppb-panel-heading > .sppb-panel-title')->each(function ($node) {
global $step, $i, $j;
$step[$i]['item'][$j++]['title'] = $node->html();
});
$h = 0;
$node->filter('.sppb-panel-body .sppb-addon-content')->each(function ($node) {
global $step, $i, $h;
$step[$i]['item'][$h++]['content'] = $node->html();
});
$i++;
});
print_r($step);
It is almost perfect except for the fact that the first element for item doesn't have number and the numbering does not reset when in new array. 除了item的第一个元素没有编号并且在新数组中编号不会重置的事实之外,这几乎是完美的。
Array
(
[0] => Array
(
[item] => Array
(
[] => Array //here no number
(
[title] => STEP 1 : ...
[content] => <p>If you are eligible to graduate...
...
[1] => Array
(
[item] => Array
(
[13] => Array //here the number should be 0
(
[title] => STEP 14 : CONVOCATION DRESS ..
[content] => <p>Here are the official...
You can see the result here: view-source: http://convo18.uum.my/ 您可以在此处查看结果:view-source: http : //convo18.uum.my/
Please help. 请帮忙。 And I interested to know if you have any elegant solution for this situation, on top of solving my problem.
除了解决我的问题外,我想知道您是否对此情况有任何优雅的解决方案。
Thanks for your time. 谢谢你的时间。
========================================================================= ================================================== =======================
UPDATE: Thanks to @NigelRen for the suggestion, here is the code that works: 更新:感谢@NigelRen的建议,以下是有效的代码:
<?php
require_once 'vendor/autoload.php';
use Goutte\Client;
$client = new Client();
$crawler = $client->request('GET', 'http://hea.uum.edu.my/index.php/academic/current-student/convocation');
$step = array();
$i = 0;
$crawler->filter('.sppb-addon.sppb-addon-accordion')->each(function ($node) use (&$step, &$i) {
$step[$i]['item'] = array();
$node->filter('.sppb-addon-title')->each(function ($node) use (&$step, &$i) {
$step[$i]['cat'] = $node->html();
});
$h = 0;
$node->filter('.sppb-panel-heading > .sppb-panel-title')->each(function ($node) use (&$step, &$i, &$h) {
$step[$i]['item'][$h++]['title'] = $node->html();
});
$h = 0;
$node->filter('.sppb-panel-body .sppb-addon-content')->each(function ($node) use (&$step, &$i, &$h) {
$step[$i]['item'][$h++]['content'] = $node->html();
});
$i++;
});
print_r($step);
Just tested a dummy setup and I think the solution is to define $j
and $h
outside any nested function. 刚刚测试了一个虚拟设置,我认为解决方案是在任何嵌套函数之外定义
$j
和$h
。 The reason being that they are not defined at global scope so when you say global $step, $i, $j;
原因是它们没有在全局范围内定义,所以当您说
global $step, $i, $j;
and then $j++
, this will take it as undefined the first time and then the post increment will set it to 1. The test code to show this is... 然后
$j++
,这将是第一次将其设为undefined,然后发布增量将其设置为1。显示此代码的测试代码是...
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
outputs... 输出...
Value=
Value=1
Whereas... 而...
$c=0;
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
Gives the desired output... 提供所需的输出...
Value=0
Value=1
So define all of these at the start... 所以在开始时就定义所有这些...
$i = 0;
$j = 0;
$h = 0;
Edit: Although as per my original comment, global
is generally frowned on, it makes testing more difficult and also (as found) may not work as you expect. 编辑:尽管按照我最初的评论,
global
通常不被接受,但它使测试更加困难,并且(如所发现的)可能无法按预期工作。 The suggested method is to use the function(...) use(...) {
method format, so in the example... 建议的方法是使用
function(...) use(...) {
方法格式,因此在示例中...
$c=0;
$a = function() use (&$c) {
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.