【发布时间】:2015-12-05 17:13:48
【问题描述】:
我在尝试迭代一些数据时遇到错误,我无法弄清楚我做错了什么。这是我从 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 次等。由于每条记录有多个 authors,我设置了一个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'])。 -
@Simba 哦,伙计,好吧,我是个白痴 - 这真是漫长的一天!用
isset($record['author']包围foreach循环,现在可以正常工作了。如果您将其作为答案发布,我将点击“接受此答案”或其他任何内容...谢谢! -
嘿,是的,我知道漫长的一天感觉很好。我已经重新发布了我的评论作为你的答案。 :)
-
我希望 apiKey 不是真实的或重要的。
-
@RocketHazmat 是的,这只是一个原型,API 密钥并不重要,只是用于测试的临时密钥
标签: php arrays json loops foreach