Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
405 views
in Technique[技术] by (71.8m points)

Count elements with a the same name in an array in php

So I have this array:

enter image description here

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

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

array_count_values

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.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...