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

Bob Patin bob at patin.com
Thu Apr 12 10:11:28 MDT 2007


Andrew,

Adding

array_unique($n);
array_unique($i);

  looks like it would work, but it still forces me to loop through  
the 1722 records. Is there a way to use

> foreach($vlResult['data']as $key=>$vlData)

to create my array, and then remove duplicates from $vlData?

It tried just doing this

foreach($vlResult['data']as $key=>$vlData)
array_unique($vlData);

but that didn't seem to work...

My knowledge of arrays is just enough to get me through most things,  
but this one has me baffled for some reason...

Thanks,

Bob






On Apr 12, 2007, at 10:53 AM, Andrew Denman wrote:

> In your original questions, it appears you were sending
> $vlData['target_name'][0] to array_unique, but $vlData 
> ['target_name'][0] is
> not an array - it's a string.  You have to write all of the strings  
> to an
> array and then run it through array_unique.
>
> I would do the following to prevent the name and image link from  
> becoming
> mismatched when you run array_unique.  This assumes that two car  
> names won't
> share the same image, as the image data is what's going to be  
> compared for
> uniqueness.
>
> foreach($vlResult['data']as $key=>$vlData) {
> 	$carlist[$vlData['target_name'][0]] =
> "/images".$reportyear."/".$vlData['target_image'][0];
> }
> array_unique($carlist);
>
> foreach($carlist as $carName => $carImage) {
>   // Output
> }
>
> If you needed the separate arrays, this should work.  It also  
> assumes that
> two car names won't share the same image, as then you can end up  
> with one
> array shorter than the other.
>
> foreach($vlResult['data']as $key=>$vlData) {
> 	$n[$c] = $vlData['target_name'][0];
> 	$i[$c] = "/images".$reportyear."/".$vlData['target_image'][0];
> 	$c++;
> }
> array_unique($n);
> array_unique($i);
>
> And let's hope this email keeps all it's line returns this time.
>
> Andrew Denman
>
> -----Original Message-----
> From: fx.php_list-bounces at mail.iviking.org
> [mailto:fx.php_list-bounces at mail.iviking.org] On Behalf Of Bob Patin
> Sent: Thursday, April 12, 2007 10:23 AM
> To: FX.php Discussion List
> Subject: [FX.php List] A little more clarity on my array_unique  
> question
>
> 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
>
> _______________________________________________
> 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