[FX.php List] A little more clarity on my array_unique question

DC dan.cynosure at dbmscan.com
Thu Apr 12 10:16:39 MDT 2007


yes, that's clearer.  you might have to back up a bit further though... 
i think the problem is really with the data model - why are those 
duplicates being returned in the first place if you are just going to 
drop them? that's where the real problem is.

maybe you need a new field called "model" that would let you search and 
group the vehicles by the model. maybe you need another related table 
that can help you "tag" vehicles as ones that all have the same image? 
maybe you just need a new relationship? a self-join on image name? since 
you are getting lots of dupes from the database result, is there a way 
to reduce the number of dupes from the source?

anyhow, array_unique() operates on an array so you have to get your data 
into an array and then run array_unique().

here's some code i've used to get data from FX into a different array 
form. maybe it will help:

http://www.dbmscan.com/snippets/index.php?cat_select=FX_and_Smarty

look at... FX nested array to recordset array.

that should get you to an array that will be "uniqueable".


also, this code has been handy in the past:
//-------------------------------------------
// CREATED: adapted from comments on php.net array_map()
// MODIFIED:  DEC
// DESCRIPTION:  get single column from nested array
// flatten into single array
function array_column($arrays, $idx) {
      foreach ($arrays as $array) {
         $newArray[] = $array[$idx];
     }
     return $newArray;
}

send it the FX data array with a field name for $idx and see what you 
get. it should be a single field's data in a column.

dan

Bob Patin had written:
> Perhaps I should explain more what I'm trying to achieve:
> 
> I'm returning an array $vlData that has auto names & image names, like 
> this:
> 
> Chevrolet Astro ---- $vlData['target_name'][0]
> Chevrolet Astro.jpg ---- $vlData['target_image'][0]
> 
> When I return this array, there can be a lot of records with the same 
> vehicle in them, and I need to display a single button for each vehicle.
> 
> So I've been using a FOREACH loop to cycle thru the results, and then 
> create a 2nd array that contains only non-duplicates.
> 
> The problem is that some models, Chevrolet particularly, have a ton of 
> matches (in this case, 1722), and the loop takes a long time to finish.
> 
> So here's my code so far; this works, but it's slow as Christmas:
> 
> <?php
> $c=0;
> //nuke the array first
> while($c<25){
>     $n[$c]='';
>     $i[$c]='';
>     $c++;
> }
> $thename='';
> $c=0;
> $n = array('thename');
> $i = array('image');
> 
> foreach($vlResult['data']as $key=>$vlData) {
>     if ($thename <> $vlData['target_name'][0]){
>         $n[$c] = $vlData['target_name'][0];
>         $i[$c] = "/images".$reportyear."/".$vlData['target_image'][0];
>         $thename = $vlData['target_name'][0];
>         $c++;
>     }
> }
> ?>
> 
> Any thoughts? :)
> 
> Thanks,
> 
> Bob
> 
> 
> 
> 
> _______________________________________________
> FX.php_List mailing list
> FX.php_List at mail.iviking.org
> http://www.iviking.org/mailman/listinfo/fx.php_list
> 


More information about the FX.php_List mailing list