我有一个递归函数,但它只返回初始数据:
$urls[] = 'http://site1.com';
$urls[] = 'http://site2.com';
foreach($urls as $url) {
$output = getMainPage($url, $zids, $listing_count, array(), array());
}
print_r($output); //Line 1
function getMainPage($url, $zids, $listing_count, $ids = array(), $names = array()) {
$dom = new DOMDocument;
@$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
$output_html = '';
foreach ($links as $link){
print_r($ids); //Line 2
$ids[] = $this_id;
$names[] = $this_name;
$listing_count++;
if(strpos($href, 'next') !== FALSE) {
$url = $next_url;
$o = getMainPage($url, $zids, $listing_count, $ids, $names);
}
}
$output['ids'] = $ids;
$output['names'] = $names;
return $output;
}
print_r($ output); //第1行只输出来自foreach $ link循环的初始数据集,但是print_r($ ids); //第2行输出增量ID。因此,当然在循环结束之前返回输出。
修改:新代码(结果相同,只有最后一组数据除外)
$urls[] = 'http://site1.com';
$urls[] = 'http://site2.com';
foreach($urls as $url) {
$output = getMainPage($url, $zids, $listing_count, array(), array());
}
print_r($output); //Line 1
function getMainPage($url, $zids, $listing_count, $ids = array(), $names = array()) {
$dom = new DOMDocument;
@$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
$output_html = '';
foreach ($links as $link){
print_r($ids); //Line 2
$output['ids'][] = $this_id;
$output['$names'][] = $this_name;
$listing_count++;
$output['listing_count'] = $listing_count;
if(strpos($href, 'next') !== FALSE) {
$url = $next_url;
$output = getMainPage($url, $output['zids'], $output['listing_count'], $output['ids'], $output['names']);
}
}
return $output;
}
答案 0 :(得分:0)
你可以试试这个:
$urls[] = 'http://site1.com';
$urls[] = 'http://site2.com';
foreach ($urls as $url) {
$output = getMainPage($url, $zids, $listing_count, array(), array());
}
print_r($output); //Line 1
function getMainPage($url, $zids, $listing_count, $ids = array(), $names = array(), $o = array()) {
$dom = new DOMDocument;
@$dom->loadHTML($html);
$links = $dom->getElementsByTagName('a');
$output_html = '';
foreach ($links as $link) {
print_r($ids); //Line 2
$output['ids'][] = $this_id;
$output['$names'][] = $this_name;
$listing_count++;
$output['listing_count'] = $listing_count;
$o[] = $output;
if (strpos($href, 'next') !== FALSE) {
$url = $next_url;
$o = getMainPage($url, $output['zids'], $output['listing_count'], $output['ids'], $output['names'], $o);
}
}
return $o;
}
答案 1 :(得分:0)
我终于明白了:
import java.lang.IllegalArgumentException
def call() {
return this
}
def cloneRepo(Map parameters = [url: null, branch: "master", credentials: null]) {
def url = parameters.getOrDefault("url", null)
def branch = parameters.getOrDefault("branch", "master")
def credentials = parameters.getOrDefault("credentials", null)
script {
if(!url) {
throw new IllegalArgumentException("cloneRepo() expects url argument to be present!")
}
if(credentials == null) {
credentials = getCredentials(url)
}
if (branch.matches("\\d+") || branch.matches("PR-\\d+")) {
if (branch.matches("PR-\\d+")) {
branch = branch.substring(3)
}
checkout changelog: false, poll: false, scm: [
$class: 'GitSCM',
branches: [[name: 'pr/' + branch]],
doGenerateSubmoduleConfigurations: false,
extensions: [[$class: 'LocalBranch', localBranch: 'pr/' + branch]],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: credentials,
refspec: 'refs/pull/' + branch + '/head:pr/' + branch,
url: url
]]
]
} else {
checkout changelog: false, poll: false, scm: [
$class: 'GitSCM',
branches: [[name: branch]],
doGenerateSubmoduleConfigurations: false,
extensions: [],
submoduleCfg: [],
userRemoteConfigs: [[
credentialsId: credentials,
url: url
]]
]
}
}
}
}