第一个元素没有数字并且数字不会在新类别中重置
First element does not have number and the number does not reset in new category
我试图抓取此页面:
http://hea.uum.edu.my/index.php/academic/current-student/convocation
这是我的代码
<?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);
除了 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...
你可以在这里看到结果:view-source:http://convo18.uum.my/
请帮忙。我很想知道除了解决我的问题之外,您是否有任何优雅的解决方案。
感谢您的宝贵时间。
============================================= ============================
更新:感谢@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);
刚刚测试了一个虚拟设置,我认为解决方案是在任何嵌套函数之外定义 $j
和 $h
。原因是它们没有在全局范围内定义,所以当您说 global $step, $i, $j;
然后 $j++
时,这将在第一次将其视为未定义,然后 post 增量将其设置为1. 显示这个的测试代码是...
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
输出...
Value=
Value=1
而...
$c=0;
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
给出所需的输出...
Value=0
Value=1
所以在开始时定义所有这些...
$i = 0;
$j = 0;
$h = 0;
编辑:
虽然根据我最初的评论,global
通常不受欢迎,但它使测试更加困难,而且(如发现的那样)可能无法按您预期的那样工作。建议的方法是使用function(...) use(...) {
方法格式,所以在例子中...
$c=0;
$a = function() use (&$c) {
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
我试图抓取此页面: http://hea.uum.edu.my/index.php/academic/current-student/convocation
这是我的代码
<?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);
除了 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...
你可以在这里看到结果:view-source:http://convo18.uum.my/
请帮忙。我很想知道除了解决我的问题之外,您是否有任何优雅的解决方案。
感谢您的宝贵时间。
============================================= ============================
更新:感谢@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);
刚刚测试了一个虚拟设置,我认为解决方案是在任何嵌套函数之外定义 $j
和 $h
。原因是它们没有在全局范围内定义,所以当您说 global $step, $i, $j;
然后 $j++
时,这将在第一次将其视为未定义,然后 post 增量将其设置为1. 显示这个的测试代码是...
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
输出...
Value=
Value=1
而...
$c=0;
$a = function() {
global $c;
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();
给出所需的输出...
Value=0
Value=1
所以在开始时定义所有这些...
$i = 0;
$j = 0;
$h = 0;
编辑:
虽然根据我最初的评论,global
通常不受欢迎,但它使测试更加困难,而且(如发现的那样)可能无法按您预期的那样工作。建议的方法是使用function(...) use(...) {
方法格式,所以在例子中...
$c=0;
$a = function() use (&$c) {
echo "Value=";
echo $c++;
echo PHP_EOL;
};
$a();
$a();