Remove duplicates in Array

Got something cool to share with AppGini users? Feel free to post it here!
Post Reply
rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Remove duplicates in Array

Post by rpierce » 2021-08-26 20:26

Hello my AppGini family,

I have created a report that is based on the Udemy course Ahmed put out to make an invoice. I have modified it and have almost got the report to look as I need. One thing that is still unfixed is how to eliminate duplicate entries in the report. Duplicates are required in the data base, but I don't want them to appear in the printed report. Below is the code I am currently using. It pulls out all the records, duplicates and all. Any suggestions are appreciated.

Code: Select all

<?php
	$haz_cont = array();
	$haz_cont_fields = get_sql_fields('haz_cont');
	$haz_cont_from = get_sql_from('haz_cont');
	$res =  sql("select {$haz_cont_fields} from {$haz_cont_from} and rfp_id={$rfp_id}", $eo);
	while($row = db_fetch_assoc($res)){
		$haz_cont[] = $row;
	}
	?>

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Remove duplicates in Array

Post by rpierce » 2021-08-27 01:46

I have tried to use array_unique in the code above but since the code is calling for all fields from the table, every record is unique since there is haz_cont_id field. If someone cold direct me on how to query the table in the code above in a way that calls for only certain fields rather than all of them my problem might be resolved.

User avatar
jsetzer
AppGini Super Hero
AppGini Super Hero
Posts: 1807
Joined: 2018-07-06 06:03
Location: Kiel, Germany
Contact:

Re: Remove duplicates in Array

Post by jsetzer » 2021-08-27 04:40

I guess there will be one or more fields in $row which you can use for detecting duplicates. There are different ways to achieve this. Those came into my mind at first:

PHP (1):
Before adding $row to $has_cont array, check if $has_cont already contains an item having the same field values. Useful function here: array_filter(). Only add $row, if $has_cont does not contain an item with identical values.

PHP (2):
On each row, build a unique $key from the key-row-values:
$key = implode('/', [$row['a'], $row['b']]);
Then add $row like this: $has_cont[$key] = $row;

Next solutions should be quite powerful because you let the database do the job and don't have to fetch all records into memory first but just get unique records.

SQL (1):
Use "distinct" SQL command in SELECT part of your query. .

SQL (2):
Use "group by" in your SQL command in combination with "first".
Kind regards,
<js />

My AppGini Blog:
https://appgini.bizzworxx.de/blog

You can help us helping you:
Please always put code fragments inside [code]...[/code] blocks for better readability

AppGini 24.10 Revision 1579 + all AppGini Helper tools

rpierce
Veteran Member
Posts: 255
Joined: 2018-11-26 13:55
Location: Washington State

Re: Remove duplicates in Array

Post by rpierce » 2021-08-27 17:41

Thank you for the ideas. I am researching on how to implement them. I am not any good at coding. I usually cut and paste to get things working. The explanations of keys is in the PHP manual are not real clear on how to use them.

I also have experimented with sql methods, but again, have no idea of the correct way to implement.

Post Reply