/    Sign up×
Community /Pin to ProfileBookmark

Excluding duplicates from array ???

hello,

I am creating an array that will often have duplicate elements it it because these elemants are common with in many rows.

I need to have only one from each group of these duplicates in my selection box. My code for ceating my selection box is below.

My question is – how do I go about excluding the unwanted rows ?

[code=php]
<?php
$sql = “SELECT account_name,SO_num,prod,SO_date FROM $client_table
WHERE account_type =’S_ORD’ ORDER BY ‘account_name,SO_num,prod'”;

$result = mysql_query($sql)
or die(“Couldn’t execute MAKE CUST LIST query.”);

while( $row = mysql_fetch_assoc( $result ) )
$sal_ord_array[] = $row;
?>

<td class = ‘cb’>
<select name=’sal_ord’>
<?php
foreach( $sal_ord_array) {
if $sal_ord_array[account_name]
foreach( $sal_ord_array as $row1 ) {
echo “<option value = “$row1[account_name]/-/$row1[SO_num]” >$row1[account_name] – $row1[SO_num]</option>”;
}// end foreach loop
?>
</select></td>
[/code]

This code creates a selection box but there are several lines of $row1[account_name] – $row1[SO_num] that are the same.

How do I just get one of each example ?
?
Thanks for you input.

to post a comment
PHP

3 Comments(s)

Copy linkTweet thisAlerts:
@scragarMay 13.2005 — [code=php]$sal_ord_array = asort($sal_ord_array);
for($i = 0; $i < count($sal_ord_array) - 1; $i++){
if($sal_ord_array[$i] == $sal_ord_array[$i+1]){
$sal_ord_array[$i] = $sal_ord_array[count($sal_ord_array)-1];
$sal_ord_array = array_pop($sal_ord_array);
};
};[/code]

Untested!
Copy linkTweet thisAlerts:
@DaveinLondonauthorMay 13.2005 — Thanks it didn't quite work.

I tried this based on the direct you gave.

[code=php]
<?php
$sql = "SELECT account_name,SO_num,prod,SO_date FROM $client_table
WHERE account_type ='S_ORD' ORDER BY 'account_name,SO_num,prod'";

$result = mysql_query($sql)
or die("Couldn't execute MAKE CUST LIST query.");

while( $row = mysql_fetch_assoc( $result ) )
$sal_ord_array[] = $row;
?>

<td class = 'cb'>
<select name='sal_ord'>
<?php
for($i = 0; $i < count($sal_ord_array) - 1; $i++){
if($sal_ord_array[$i] != $sal_ord_array[$i+1]){
echo "<option value = "$sal_ord_array[account_name]/-/$sal_ord_array[SO_num]" >$sal_ord_array[account_name] - $sal_ord_array[SO_num]</option>";
} // end else
} // end for loop
?>
</select></td>

[/code]



But again it isn't working - I think it is close though ! ?

Any more thoughts ???
Copy linkTweet thisAlerts:
@NogDogMay 13.2005 — if the query is only being used to populate the select options, then only select the columns you need and use DISTINCT in your query:
[code=php]
$sql = "SELECT DISTINCT account_name,SO_num FROM $client_table
WHERE account_type ='S_ORD' ORDER BY 'account_name,SO_num,prod'";
[/code]

If you need those other columns elsewhere, then create an array of just the stuff you need, then get rid of duplicates:
[code=php]
while($row = mysql_fetch_assoc($result))
{
$optionsAll[] = "{$row[account_name]}/-/{$row[SO_num]}";
}
$options = array_unique($optionsAll)
echo "<select name='sal_ord'>n";
foreach($options as $value)
{
echo "<option value='$value'>$value</option>n";
}
echo "</select>n";
[/code]
×

Success!

Help @DaveinLondon spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.16,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...