Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
Groups > comp.lang.php > #9258
| From | Denis McMahon <denismfmcmahon@gmail.com> |
|---|---|
| Newsgroups | comp.lang.php |
| Subject | Re: Unique array of arrays - how can I do this? |
| Date | 2013-05-04 12:25 +0000 |
| Organization | A noiseless patient Spider |
| Message-ID | <km2un0$ole$2@dont-email.me> (permalink) |
| References | <e6f6a70e-e5a3-435b-b354-273c2386153e@googlegroups.com> |
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@gmail.com
Back to comp.lang.php | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
Unique array of arrays - how can I do this? thomas.lehmann.private@googlemail.com - 2013-05-04 04:51 -0700 Re: Unique array of arrays - how can I do this? The Natural Philosopher <tnp@invalid.invalid> - 2013-05-04 13:20 +0100 Re: Unique array of arrays - how can I do this? Denis McMahon <denismfmcmahon@gmail.com> - 2013-05-04 12:25 +0000 Re: Unique array of arrays - how can I do this? Thomas 'PointedEars' Lahn <PointedEars@web.de> - 2013-05-06 00:06 +0200
csiph-web