我是Sharam,欧洲现任交换生,来自一个解决政治问题的课后俱乐部。关于东南亚青年的问题。在我们的团队中,我们每个月都会在世界各地招募新的志愿者,我们已经设置了中央服务器来进行分类和分类。优先考虑俱乐部未来必须履行的任务,以便在我们国家具有意义。我对PHP知之甚少,但我渴望学习,这是我的问题......
因为SQL过于复杂,我们使用CSV。每个人都可以编辑主CSV&像这里一样添加任务:
Priority,Task,Person
High,Campus flyers,Ashley
High,Test videocall with teacher,Sharam
Low,New Website,Martin & Aneet
Medium,Auditorium recruiting,Martin & Aneet & Ashley
Low,Garbage collecting,Sharam & Ashley
High,Ask permission to reserve classroom Sat & Sun,Sharam
如您所见,项目的优先级以任何顺序为高,中,低。我尝试了array_merge_recursive(),但也许我做错了:
<?php
$file = @fopen("tasks-list.csv", "r") or exit("error, check server status");
$array = array();
$header = fgetcsv($file, 1000, ","); //don't need header in array
while (!feof($file)) {
$data = fgetcsv($file, 1000, ",");
if (!empty($data)) {
$array = array_merge_recursive($array, array($data[0] => $data[1], $data[2]));
}
}
fclose($file);
?>
结果混淆了,我不知道如何按列分组CSV输入:
High priority tasks:
1. Campus flyers (Ashley)
2. Test videocall with teacher (Sharam)
3. Ask permission to reserve classroom Sat & Sun (Sharam)
Medium priority tasks:
1. Auditorium recruiting (Martin & Aneet & Ashley)
Low priority tasks:
1. New Website (Martin & Aneet)
2. Garbage collecting (Sharam & Ashley)
我的代码缺少表现部分,但这不是困难的部分。 HTML对我来说很容易,但我有正确分组的问题。我怎样才能实现它?谢谢
对我的语法和对不起拼写,我刚刚学习英语第二年! --Sharam
答案 0 :(得分:1)
查看我的PHP课程:http://pastebin.com/KVcYxuXY
以下是它的使用方法
<?php
require_once('CSV_parser.php');
// path to your csv file
$parser = new CSV_parser('c:/sample.csv');
// Result an array
$parser->toArray();
// To table
$parser->toTable();
// To JSON
$parser->toJSON();
?>
答案 1 :(得分:0)
构建一个以最左列为索引的数组,该数组中的每个条目都是该行中的其余数据。
$result = array();
while (!feof($file)) {
$data = fgetcsv($file, 1000, ",");
$key = array_shift( $data);
if( !isset( $result[ $key ])) {
$result[ $key ] = array();
}
$result[ $key ][] = $data;
}
现在,要打印出来,这就是你要做的全部:
foreach( $result as $priority => $entries) {
echo $priority . ' priority tasks' . "\n";
foreach( $entries as $i => $entry) {
echo "$i. {$entry[0]} ({$entry[1]})\n";
}
echo "\n";
}