[英]How to read a range of rows from CSV file to JSON array using PHP to handle large CSV file?
[英]Using php, how can we read 5 rows from a csv file, call another function and then read next 5 rows and repeat this?
下面是我的代码。 此代码读取的csv文件包含大约100个学生个人资料。 这些配置文件在此HTML页面-http: //www.depts.ttu.edu/honors/medallion-ceremony/test/中以幻灯片显示。 我想为每5个个人资料显示一个广告幻灯片。 请帮忙 !
$profiles = csv_to_array($_SERVER['DOCUMENT_ROOT'].'/...../profiles.csv');
function csv_to_array($filename, $delimiter=',', $enclosure='"', $escape = '\\')
{
if(!file_exists($filename) || !is_readable($filename)) return false;
$header = null;
$data = array();
$lines = file($filename);
foreach($lines as $line) {
$values = str_getcsv($line, $delimiter, $enclosure, $escape);
if(!$header)
$header = $values;
else
$data[] = array_combine($header, $values);
}
return $data;
}
function displayProfiles($limit=100)
{
global $profiles;
$count = 1;
foreach ($profiles as $profile)
{
if ($count <= $limit)
{
displayProfile($profile);
$count++;
}
}
}
function displayProfile($profile) {.....}
您可以在哪里签入displayProfiles
函数。 使用取模运算符http://php.net/manual/en/language.operators.arithmetic.php
然后,您可以在循环中执行以下操作:
if($count % 5 == 0) {
displayAdvertisement();
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.