On Fri, 23 Jan 2004, NIPP, SCOTT V (SBCSI) wrote:
> I am trying to populate an array from a MySQL database. I think I
> have the code correct to populate the array, but I need to somehow sort the
> array eliminating duplicate values. I think I can loop through the array
> doing a comparison and building a new array by discarding the value if it is
> a duplicate, but I was not sure if there was a more efficient way of doing
> this. I am already looping through the database query results, and I am
> just thinking about efficiency here.
First, you should see if you can optimize your SQL query to do the work
for you, a la SELECT DISTINCT and/or ORDER BY.
Beyond that, try using an associative array. It cannot contain duplicate
keys, and it can be sorted by key. For example, assume your data is:
$dbdata = array(
"userid" => 10,
"display" => "Smith, Joan",
"username" => "jsmith",
"location" => "Kalamazoo" );
$keydata = $dbdata['display'] . $dbdata['userid']; // key format
$storage[$keydata] = $dbdata; // store data
ksort($storage); // sort array by key
foreach($storage as $v) { print_r($v); } // loop sorted values
In the case that you might have two Joan Smiths, the key is the display
name and the userid concatenated. It can be anything that sorts well...
> <?php
> do {
> $entry = $list['id_sys'];
> $id = split('-', $list['id_sys'], 1);
> $sbcuid = $id[0];
> $users[] = '$sbcuid';
> } while ($list = mysql_fetch_assoc($result));
> ?>
Some problems here... you might try while() { } instead of do { } while()
which makes your loop run once without $list being set. Also, list is a
reserved word, so even if it works as a variable $list, don't do that...
Further, you mean $users[] = $sbcuid; not $users[] = '$sbcuid';
--
Kelly Hallman
// Ultrafancy
--
PHP Database Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php