我在尝试迭代某些数据时遇到错误,但我无法弄清楚我做错了什么。这是我从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
每条记录,我设置了一个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
循环中取得的所有作者,所以我收到错误似乎很奇怪。
答案 0 :(得分:2)
您确定每条记录都包含一个作者元素吗?
也许你应该在foreach()循环之前添加一个ifset($ record [&#39; author&#39;])。
(作为OP的要求重新发布评论)