php - 使用循环对数据中的项目和计数进行分组
问题描述
我有数据时
var_dump ($data);
这是我的数据的显示方式($data 不是表格应答器):
+-------+----------+-----------+---------------+
| adan | Afla | avo | 0 |
+-------+----------+-----------+---------------+
| adan | ken | joly | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 0 |
+-------+----------+-----------+---------------+
但是,我希望表格显示变为:
+-------+----------+-----------+---------------+
| adan | Afla | avo | 1 |
+-------+----------+-----------+---------------+
| adan | ken | joly | 1 |
+-------+----------+-----------+---------------+
| busia | koko | ho | 3 |
+-------+----------+-----------+---------------+
这是我的代码:
foreach($data as $liste){
$town[]=$liste->getLocalite()->getName();
$counties[]=$liste->getCountie()->getName();
$districts[]=$liste->getDistrict()->getName();;
}
$a= count($listforages)-1;
$numberDoc=0;
$tab1=$tab2=$tab3=$tab4=array();
for ($i=0; $i<count($data);$i++){
$tab[$i]= $town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'.$numberDoc[$i];
if( $i == $a ) {
if ($counties [$a-1]==$counties [$a] && $districts[$a-1]==$districts[$a] && $numberDoc[$a-1]==$numberDoc[$a] ){
$numberDoc[$a]++; // counter
$tab1[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
}
if ($counties [$a-1] !=$counties [$a] && $districts[$a-1] !=$districts[$a] && $numberDoc[$a-1] !=$numberDoc[$a] ){
$numberDoc[$a]++; // counter
$tab2[$a-1]=$town[$a-1].'--'.$counties [$a-1].'--'.$districts[$a-1].'--'. $numberDoc[$a-1];
}
}
else{
if ($counties [$i]==$counties [$i+1] && $districts[$i]==$districts[$i+1] && $numberDoc[$i]==$numberDoc[$i+1] ){
$numberDoc[$i]++; // counter
$tab3[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
}
if ($counties [$i] !=$counties [$i+1] && $districts[$i] !=$districts[$i+1] && $numberDoc[$i] !=$numberDoc[$i+1] ){
$numberDoc[$i]++; // counter
$tab4[$i]=$town[$i].'--'.$counties [$i].'--'.$districts[$i].'--'. $numberDoc[$i];
}
}
}
当你看到我的数据时(见表格):
town 是第 0 列,counties 是第 1 列,区是第 2 列,numberDoc 是第 3 列,
我的代码不起作用,如何解决这个问题?
注意:原谅我的英语,
先感谢您
解决方案
推荐阅读
- python-3.x - 如何在 Tkinter 中添加 Matplotlib 图形(比萨图样式)
- c++ - 为什么 cin.fail() 不评估用户输入是否为 int?
- ios - 应用启动时,iOS 是否会将所有系统动态框架加载到内存中?
- c - 如何修复 mac OS 中的 [-Wreturn-type] 错误
- android - Trying to populate my spinner with data from Firebase Realtime Database but my spinner is blank
- ruby-on-rails - 如果我直接点击“localhost:3000/404”端点,Rails config.exceptions_app 将不起作用
- c - 如何使用指针在 C 中的第一个空间上拆分字符串?
- windows - 如何在带有 gui 的 msys2 中使用 emacs
- react-native - 如何在 react-native 中创建变量并将其设置为多行 JSX 结构
- google-chrome - 带过滤器的跨浏览器 SVG 动画(Chrome 可以,不是 Firefox/Safari)