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
309 views
in Technique[技术] by (71.8m points)

php - How to find duplicate array values from PDO fetchall?

I am working on a reporting dashboard for our company intranet site. The function below is selecting all of the jobs and suffix from a table. I'm using PDO fetchall to retrieve my results. the array looks like:

Array
( 
    [0] => Array 
    ( 
        [JOB] => 197182 
        [SUFFIX] => 002 
    ) 
    [1] => Array 
    ( 
        [JOB] => M03001 
        [SUFFIX] => 001 
    ) 
    [2] => Array 
    ( 
        [JOB] => 197182 
        [SUFFIX] => 002 
    )
)

In this case, the dashboard should have 2 rows, 1 for each job (excluding the duplicate) however how can I search through this array and have it only return a row count of 2 instead of 3? Since there is a duplicate job and suffix I don't want to create a third row with the same job-suffix so it should only be two rows.

function row_count() 
{
    $conn = new PDO('odbc:GLOBALTST');
    $result = $conn->prepare('SELECT JOB, SUFFIX FROM JOBS_IN_PROCESS_G');
    $result->execute();
    $row = $result->fetchall(PDO::FETCH_ASSOC);
    global $count;
    $count = count($row);\ if I print this out I get 3
}
question from:https://stackoverflow.com/questions/66049884/how-to-find-duplicate-array-values-from-pdo-fetchall

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

1 Answer

0 votes
by (71.8m points)

You can fix that on your query side by selecting DISTINCT rows of JOB and SUFFIX column like below,

$result = $conn->prepare('SELECT DISTINCT JOB, SUFFIX FROM JOBS_IN_PROCESS_G');

OR if you want on PHP side after selecting rows from table then check this example , But I prefer first one as it is better to get only what you need from DB table.


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

...