计算php数组中同名的元素 [英] Count elements with a the same name in an array in php
问题描述
所以我有这个数组:
我不知道是否清楚,但是这个数组中有很多同名的元素,我想统计这些元素并显示出来,这就是我创建数组的方式:
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']);打印_r($row);}
我已经试过了:
这个 anwser 似乎对我不起作用,但也许我做错了什么.
btw $rows['FC_RAYON'] 来自另一个查询,它与这个问题无关,但如果你想看到它,我会编辑我的帖子以显示它.
那我必须这样做吗?你能帮忙吗.
编辑
更好地查看数组:
数组 ( [0] => 2 [1] => RT-SCB-PB01 )阵列([0] => 0 [1] => RT-SCB-PB01)阵列([0] => 3 [1] => RT-SCB-PB01)阵列([0] => 1 [1] => ASDC-PBSN)阵列([0] => 0 [1] => ASDC-PBSN)阵列([0] => 0 [1] => ASDC-PBSN)阵列([0] => 0 [1] => ASDC-PBSN)阵列([0] => 1 [1] => ASDW-PBSN)
这是数组中很小的一部分.
编辑 2
数组([0] =>2[1] =>RT-SCB-PB01)大批([0] =>2[1] =>RT-SCB-PB01)大批([0] =>1[1] =>RT-SCB-PB01)大批([0] =>3[1] =>RT-SCB-PB01)大批([0] =>2[1] =>ASDC-PBSN)大批([0] =>2[1] =>ASDC-PBSN)大批([0] =>1[1] =>ASDC-PBSN)大批([0] =>1[1] =>ASDW-PBSN)大批([0] =>0[1] =>ASDW-PBSN)大批([0] =>0[1] =>ASDW-PBSN)
所以这是对数组的一个更好的观察,数组中的数据是不同的,因为它是一个非常活跃的数据库
编辑 3
如果您有兴趣,这是我的全部结果,您可以在此处查看:
https://mega.nz/#!uvpBWSoL!V6xYCuJ5mCWYqynPio用油漆打开
array_count_values() 返回一个数组,使用数组的值作为键,并使用它们在数组中的频率作为值.
$varArray = array();//取一个空数组foreach ($rowa 作为 $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']);//我也可以认为 $rows['FC_RAYON'] 是像 RT-SCB-PB01、ASDC-PBSN 等那样给出值.array_push($varArray,$row[1]);//我已经推送了 RT-SCB-PB01、ASDC-PBSN 等数组的发送值,并在单个数组中制作.}$dupArrays = array_count_values($varArray);//它将返回 Counts 数组的所有值echo 'Total No Items: '.count($dupArrays).'<br><br>';echo "";打印_r($dupArrays);echo "</pre>";
输出将是:
总共没有项目:3大批([RT-SCB-PB01] =>3//RT-SCB-PB01 的重复值计数为 3[ASDC-PBSN] =>4//ASDC-PBSN 重复值个数为 3[ASDW-PBSN] =>1//ASDW-PBSN 的重复值计数为 3)
使用foreach方法获取
foreach($dupArrays as $key => $value){回声 $key.'计数 '.$value.'次.';echo "
";}
输出:
RT-SCB-PB01 计数 3 次.ASDC-PBSN 计数 4 次.ASDW-PBSN 计数 1 次.
So I have this array:
I don't know if it's clear, but there are alot of elemenst in this array that have the same name, I want to count this elements and show it, this is how I create the 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);
}
I already have tried this:
count of duplicate elements in an array in php
This anwser doesn't seem to work for me, but maybe I am doing something wrong.
btw $rows['FC_RAYON'] is from another query, it's not relevant to this question, but if you want to see it I will edit my post to show it.
So must I do? could you please help.
EDIT
better look at the array:
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 )
This is a very small part of the array.
EDIT 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
)
so this is here is an even beter look at the array, the data in the array is diffrent because it's is a very actief database
EDIT 3
if your intersted this is my entire result you can see it here:
https://mega.nz/#!uvpBWSoL!V6xYCuJ5mCWwiYqnoaz6LiYynioCylWDxPYioV_9qpo open with paint
array_count_values() returns an array using the values of array as keys and their frequency in array as values.
$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>";
The output will be :
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
)
Get using foreach method
foreach($dupArrays as $key => $value){
echo $key.' Count '.$value.' times.';
echo "<br>";
}
Output:
RT-SCB-PB01 Count 3 times.
ASDC-PBSN Count 4 times.
ASDW-PBSN Count 1 times.
这篇关于计算php数组中同名的元素的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!