我得到一个错误,而试图迭代一些数据,我无法找出我做错了什么。这是我从API请求返回的数据格式:
Array
(
[search-results] => Array
(
[entry] => Array
(
[0] => Array
(
[author] => Array
(
[0] => Array
(
[authname] => Griffin J.
[surname] => Griffin
[initials] => J.M.
)
[1] => Array
(
[authname] => Williams D.
[surname] => Williams
[initials] => D.H.
)
)
)
[1] => Array
( ...etc...
)
)
)
)
供参考,以上数据输出来自以下代码中的$eachJson
。
我一次只能做一个API请求来获得100个结果,所以我设置了一个for
循环来执行搜索100次,然后再执行100次,等等。由于每条记录有多个author
s,我设置了一个foreach
循环来迭代作者,这就是我得到错误消息的地方:
Notice: Undefined index: author in C:'xampp'htdocs'academic_intelligence'public'ScopusTest.php on line 42
Warning: Invalid argument supplied for foreach() in C:'xampp'htdocs'academic_intelligence'public'ScopusTest.php on line 42
代码如下:
$apiKey = "&apiKey=c2cb86c3a511ed34dd6f03f481c637c1";
$search1 = urlencode("badgers");
$search2 = urlencode(" OR weasels");
$start = 0;
$scopusData = [];
// create an array to represent citation values to ignore, i.e. not interested
// in any publications with less than 4 citations
$ignore = array(0, 1, 2, 3);
// set processing time for browser before timeout
ini_set('max_execution_time', 3600);
// override default PHP memory limit
ini_set('memory_limit', '-1');
// REST HTTP GET Request searching for people associated with keywords (term)
$searchLink = "http://api.elsevier.com/content/search/scopus?query=KEY(" . $search1 . $search2 . ")" . $apiKey . "&sort=citedby-count&count=100&start=" . $start . "&view=complete";
// save results to a variable
$searchResponse = file_get_contents($searchLink);
// convert JSON to PHP variable
$searchJson = json_decode($searchResponse, true);
// get total number of results for query to know when to stop iterating data
$total = $searchJson['search-results']['opensearch:totalResults'];
// iterate data loading next 200 results (max) each time and adding new results to array
for ($i = $start; $i <= $total; $i+=100) {
// REST HTTP GET Request searching for people associated with keywords (term)
$eachLink = "http://api.elsevier.com/content/search/scopus?query=KEY(" . $search1 . $search2 . ")" . $apiKey . "&sort=citedby-count&count=100&start=" . $i . "&view=complete";
// save results to a variable
$eachResponse = file_get_contents($eachLink);
$eachJson = json_decode($eachResponse, true);
foreach ($eachJson['search-results']['entry'] as $record) {
// array to store authors
$authors = [];
foreach ($record['author'] as $thisAuthor) { // **LINE 42**
// push initials and surname to array
array_push($authors, ($thisAuthor['initials'] . $thisAuthor['surname']));
};
// scopus ID
$scopusID = $record['dc:identifier'];
// paper title
$title = $record['dc:title'];
// date
$date = substr($record['prism:coverDate'], 0, 4);
// citations, if less than 4 then break out of iteration
if (!in_array(($cites = $record['citedby-count']), $ignore)) {
$cites = $record['citedby-count'];
} else {
break 2;
}
$thisData = [
"authors" => $authors,
"ID" => $scopusID,
"title" => $title,
"date" => $date,
"cites" => $cites
];
array_push($scopusData, $thisData);
}
};
// need to replace single quotes to avoid char escape
for ($i = 0; $i < count($scopusData); $i++) {
foreach ($scopusData[$i]['authors'] as &$edit) {
$edit = str_replace("'", "", $edit);
};
$scopusData[$i]['title'] = str_replace("'", "", $scopusData[$i]['title']);
};
我突出显示了导致错误的第42行。这一定是一个简单的问题,但这是漫长的一天,我不能找出问题所在!我仍然在最后得到正确的数据,最后的数组scopusData
包括从错误的foreach
循环中获取的所有作者,所以我得到错误似乎很奇怪。
您确定每个记录都包含一个author元素吗?
也许你应该在foreach()循环之前添加一个ifset($record['author'])。
(转发评论作为OP的请求的答案)