所以我有这个数组:
我不知道是否清楚,但是此数组中有很多elemenst具有相同的名称,我想对这个元素进行计数并显示出来,这就是我创建数组的方式:
foreach ($rowa as $rowsa)
{
$sql = "SELECT count(*) as NUMBER FROM BANDZENDINGEN WHERE FB_AFGESLOTEN = 'F' AND FB_AKTIEF = 'T' AND FI_AFVOERKANAAL = 1 AND FI_RAYONID = $rowsa AND FI_VERRIJKINGID < 1;";
$sfh = $dbh->prepare($sql);
$sfh->execute();
$row = $sfh->fetchAll(PDO::FETCH_COLUMN, 0);
array_push($row, $rows['FC_RAYON']);
print_r($row);
}
我已经尝试过:
count of duplicate elements in an array in php
这个答案似乎不适用于我,但也许我做错了事。
btw $ rows ['FC_RAYON']来自另一个查询,与这个问题无关,但是如果您想看到它,我将编辑我的帖子以显示它。
那我该怎么办?你能帮忙吗?
编辑
更好地看一下数组:
Array ( [0] => 2 [1] => RT-SCB-PB01 )
Array ( [0] => 0 [1] => RT-SCB-PB01 )
Array ( [0] => 3 [1] => RT-SCB-PB01 )
Array ( [0] => 1 [1] => ASDC-PBSN )
Array ( [0] => 0 [1] => ASDC-PBSN )
Array ( [0] => 0 [1] => ASDC-PBSN )
Array ( [0] => 0 [1] => ASDC-PBSN )
Array ( [0] => 1 [1] => ASDW-PBSN )
这是数组的一小部分。
编辑2
Array
(
[0] => 2
[1] => RT-SCB-PB01
)
Array
(
[0] => 2
[1] => RT-SCB-PB01
)
Array
(
[0] => 1
[1] => RT-SCB-PB01
)
Array
(
[0] => 3
[1] => RT-SCB-PB01
)
Array
(
[0] => 2
[1] => ASDC-PBSN
)
Array
(
[0] => 2
[1] => ASDC-PBSN
)
Array
(
[0] => 1
[1] => ASDC-PBSN
)
Array
(
[0] => 1
[1] => ASDW-PBSN
)
Array
(
[0] => 0
[1] => ASDW-PBSN
)
Array
(
[0] => 0
[1] => ASDW-PBSN
)
所以这是对数组的更好的观察,数组中的数据是不同的,因为它是一个非常活跃的数据库
编辑3
如果您对此有兴趣,可以在这里查看:
https://mega.nz/#!uvpBWSoL!V6xYCuJ5mCWwiYqnoaz6LiYynioCylWDxPYioV_9qpo 用油漆打开
答案 0 :(得分:4)
array_count_values()返回一个数组,该数组使用array的值作为键,并以其在array中的频率作为值。
$varArray = array(); // take a one empty array
foreach ($rowa as $rowsa)
{
$sql = "SELECT count(*) as NUMBER FROM BANDZENDINGEN WHERE FB_AFGESLOTEN = 'F' AND FB_AKTIEF = 'T' AND FI_AFVOERKANAAL = 1 AND FI_RAYONID = $rowsa AND FI_VERRIJKINGID < 1;";
$sfh = $dbh->prepare($sql);
$sfh->execute();
$row = $sfh->fetchAll(PDO::FETCH_COLUMN, 0);
array_push($row, $rows['FC_RAYON']);
//print_r($row);
//array_push($varArray,$rows['FC_RAYON']); // Also May I think $rows['FC_RAYON'] is give value like RT-SCB-PB01,ASDC-PBSN etc.
array_push($varArray,$row[1]); // I have push the send value of array like RT-SCB-PB01,ASDC-PBSN etc. and make in single array.
}
$dupArrays = array_count_values($varArray); // It will return Counts all the values of an array
echo 'Total No Items: '.count($dupArrays).'<br><br>';
echo "<pre>";
print_r($dupArrays);
echo "</pre>";
输出将是:
Total No Items: 3
Array
(
[RT-SCB-PB01] => 3 // Count of duplicate value of RT-SCB-PB01 is 3
[ASDC-PBSN] => 4 // Count of duplicate value of ASDC-PBSN is 3
[ASDW-PBSN] => 1 // Count of duplicate value of ASDW-PBSN is 3
)
使用foreach方法获取
foreach($dupArrays as $key => $value){
echo $key.' Count '.$value.' times.';
echo "<br>";
}
输出:
RT-SCB-PB01 Count 3 times.
ASDC-PBSN Count 4 times.
ASDW-PBSN Count 1 times.
答案 1 :(得分:0)
您不能只执行一个查询吗?
$sql = "SELECT FI_RAYONID,COUNT(FI_RAYONID) as NUMBER
FROM BANDZENDINGEN
WHERE FB_AFGESLOTEN = 'F'
AND FB_AKTIEF = 'T'
AND FI_AFVOERKANAAL = 1
AND FI_VERRIJKINGID < 1
GROUP BY FI_RAYONID ;";
$sfh = $dbh->prepare($sql);
$sfh->execute();
$result = $sfh->fetch(PDO::FETCH_KEY_PAIR);
然后$ result成为ID和计数的键控数组,因此您可以更轻松地使用它