FUDforum
Fast Uncompromising Discussions. FUDforum will get your users talking.

Home » Imported messages » comp.lang.php » Unique array of arrays - how can I do this?
Show: Today's Messages :: Polls :: Message Navigator
Return to the default flat view Create a new topic Submit Reply
Re: Unique array of arrays - how can I do this? [message #181313 is a reply to message #181308] Sat, 04 May 2013 12:25 Go to previous messageGo to previous message
Denis McMahon is currently offline  Denis McMahon
Messages: 634
Registered: September 2010
Karma:
Senior Member
On Sat, 04 May 2013 04:51:28 -0700, thomas.lehmann.private wrote:

> Hi,
>
> While calculation I get a solution as an array of integers. There're
> many solutions and I would like to keep them in an array.
>
> Because of the algorithm there are different paths to get to the same
> solution but I want to have it once only.
>
> I know that I can use array_unique but that's too slow when called too
> often. I can't wait until end of calculation because then I run into
> memory problems.
>
> So I need an efficient way to add elements once only to an array while
> calculation with an array as element.
>
> How can I do this?

Given that your answers are integers, at the point that you put your
answers into the array, assuming that the answer is in $ans:

if ( !isset( $arr ) )
$arr = array();
$arr[$ans] = $ans;

When you finish calculations:

sort( $arr );
foreach ( $arr as $k=>$v )
echo "Answer {$k} is {$v}.\n";

or:

sort( $arr );
echo "The answers are: " . implode( $arr, ", " ) . ".\n";

--
Denis McMahon, denismfmcmahon(at)gmail(dot)com
[Message index]
 
Read Message
Read Message
Read Message
Read Message
Previous Topic: replacing spaces with newline
Next Topic: changing video source from youtube to my site
Goto Forum:
  

-=] Back to Top [=-
[ Syndicate this forum (XML) ] [ RSS ]

Current Time: Wed Nov 27 23:50:52 GMT 2024

Total time taken to generate the page: 0.04165 seconds