Jump to content

Help with making array unique


TheBrandon

Recommended Posts

Right now I have an array of zipcodes with open invitations; I want to loop over the array of zipcodes, query the database and get the name of each zipcode and then remove duplicate names.

 

So if 90210, 90211 and 90212 are in the array with the first 2 being City A and the last one being City B, I just want City A and City B, not City A, City A, City B.

 

Right now I'm doing this:

foreach($invitation_Zipcodes as $key => $value){
echo 'Key: '.$key.' Value: '.$value.'<br/>';

$csaname_sql = "SELECT CBSA_Name FROM ZIPCodes WHERE ZipCode = '$value'";
$csaname_result = mysql_query($csaname_sql);
$csaname_array[] = mysql_fetch_assoc($csaname_result);

}

//$csaname_array = array_unique($csaname_array);

print_r($csaname_array);
echo '<hr/>';
print_r($invitation_Zipcodes);
exit;

 

That outputs this:

Array

(

    [0] => Array

        (

            [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL

        )

 

    [1] => Array

        (

            [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL

        )

 

    [2] => Array

        (

            [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL

        )

 

    [3] => Array

        (

            [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL

        )

 

    [4] => Array

        (

            [CBSA_Name] => Pensacola-Ferry Pass-Brent, FL

        )

 

)

<hr/>Array

(

    [0] => 32548

    [1] => 32579

    [2] => 32578

    [3] => 32580

    [7] => 32501

)

 

If I uncomment the unique call, it outputs this:

Array

(

    [0] => Array

        (

            [CBSA_Name] => Crestview-Fort Walton Beach-Destin, FL

        )

 

)

<hr/>Array

(

    [0] => 32548

    [1] => 32579

    [2] => 32578

    [3] => 32580

    [7] => 32501

)

 

What am I doing wrong? Why is it losing "Pensacola-Ferry Pass-Brent, FL" ?

Link to comment
Share on other sites

You can do this all directly in your query -

$csaname_sql = "SELECT DISTINCT CBSA_Name FROM ZIPCodes WHERE ZipCode IN ('".implode("','",$invitation_Zipcodes)."')";

 

There's almost never a time when you should put a SELECT query inside of a loop, due to the performance hit you take. There is always a way to get one query to retrieve all the rows you want at one time.

 

Link to comment
Share on other sites

Use the IN clause to do this all in a single query. Use the DISTINCT clause to select unique values only.

 

Here's an example. Don't copy this code into a production environment, use it for educational purposes

 

<?php

$zips = array( '90210','90211','90212' );

$inString = '"' . implode('","', $zips) . '"';

$query = 'SELECT DISTINCT CBSA_Name FROM ZIPCodes WHERE ZipCode IN ('.$inString.')';

echo 'The query will be: '.$query.'<br>';

$result = mysql_query($query);
if( !$result )
die('Error: '.mysql_error());

echo '<h3>Results</h3>';
while($row = mysql_fetch_row($result))
echo $row[0].'<br>';

?>

 

edit - What he said.

Link to comment
Share on other sites

I'll definitely try the solutions you have recommended. Thanks both of you.

 

In an effort to further understand how the array_unique command works, could you still tell me why that wasn't working the way I wanted?

 

I fully intend to switch to your SQL version of the code, I'd just like to learn why it didn't work for future projects.

Link to comment
Share on other sites

To elaborate, array_unique is only meant for a single-dimensional array. You've tried to use a two-dimensional array.

 

<?php

$array1 = array( 'Apple','Banana','Apple','Orange' );
$array2 = array(
array('Apple'),
array('Banana'),
array('Apple'),
array('Orange')
);

header('Content-Type: text/plain'); // Display page as text, not html

echo 'Array 1: ';
print_r($array1);

echo 'Array 2: ';
print_r($array2);

echo 'Array 1 Unique: ';
print_r(array_unique($array1));

echo 'Array 2 Unique: ';
print_r(array_unique($array2));

?>

 

Because every array within $array2 resolves to the string 'Array', it sees them all as identical.

Link to comment
Share on other sites

Array_unique is not working because you are storing the array that mysql_fetch_assoc returns into your main array, not just the CBSA_Name value out of the array.

 

Could you elaborate on how to just pull the CBSA_Name value?

 

I'm still learning a lot when it comes to manipulating arrays so I think this is something I do frequently and would like to know how to do properly.

Link to comment
Share on other sites

To elaborate, array_unique is only meant for a single-dimensional array. You've tried to use a two-dimensional array.

 

<?php

$array1 = array( 'Apple','Banana','Apple','Orange' );
$array2 = array(
array('Apple'),
array('Banana'),
array('Apple'),
array('Orange')
);

header('Content-Type: text/plain'); // Display page as text, not html

echo 'Array 1: ';
print_r($array1);

echo 'Array 2: ';
print_r($array2);

echo 'Array 1 Unique: ';
print_r(array_unique($array1));

echo 'Array 2 Unique: ';
print_r(array_unique($array2));

?>

 

Because every array within $array2 resolves to the string 'Array', it sees them all as identical.

 

Thank you so much for your in-depth reply. I definitely understand what you're saying. I didn't realize array_unique was solely meant for single-dimension arrays. Thanks again!

Link to comment
Share on other sites

To elaborate on PFMaBiSmAd's comment, you want to use this code instead:

 

<?php

foreach($invitation_Zipcodes as $key => $value){
echo 'Key: '.$key.' Value: '.$value.'<br/>';

$csaname_sql = "SELECT CBSA_Name FROM ZIPCodes WHERE ZipCode = '$value'";
$csaname_result = mysql_query($csaname_sql);
$csaname_data = mysql_fetch_row($csaname_result);
$csaname_array[] = $csaname_data[0];

}

print_r($csaname_array);

?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.