RE: Recursive function for array task?

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 




> -----Original Message-----
> From: Stewart Macdonald [mailto:s.macdonald@xxxxxxxxxxxxx]
> Sent: 27 April 2007 10:13
> To: php-general@xxxxxxxxxxxxx
> Subject:  Recursive function for array task?
>
>
> Hi all,
>
> I've got a bit of a logic problem that I can't figure out.
>
> I've got an array:
>
> Array
> (
>      [B] => X
>      [C] => Q,K
>      [D] => M
>      [F] => V
>      [G] => N
>      [I] => X,M
>      [K] => C
>      [M] => I,V
>      [Q] => C
>      [R] => U
>      [V] => M
>      [X] => I
> )
>
> This array shows groupings of objects:
>   B is paired with X
>   X is also paired with I
>   I is also paired with M
>   M is paired with V and D
>   V is paired with F
> F and D aren't paired with anything else, so that's the end of one
> group.
> There are a few other pairs in the array. So this array contains the
> following groups:
>   B,X,I,M,V,F,D
>   C,Q,K
>   G,N
>   R,U
>
> The general structure of each element in the array is:
> [key] -> code1, code2, code3, ..... codeN
>
> N is theoretically unlimited (but practically limited to about 50).
> The key can be a value in the array multiple times, but each key
> should be unique.
>
> It's easy to look at the array and manually work out these groups,
> but I can't find a way to programatically work out groups.
>
> I've tried to write a recursive function to do it. The function works
> most times, but it's stalling on the above array.
>
> Does PHP have an array function that would let me 'collapse' all
> these together somehow? If it makes it easier (i.e., there's a way of
> doing this if the original array is formatted differently) I can re-
> write the code that generates the original array.
>
> I'm sure I'm overlooking something simple. Can anyone offer any help?
> I don't want to manually work out groups from thousands of arrays!
>
>
> Thanks!
>
> Stewart
>

Interesting problem. I've got a few ideas but please could you clarify what
you want as the output? Is it just an array of the groups, i.e.

Array
(
	[0] => 'B,X,I,M,V,F,D'
	[1] => 'C,Q,K'
	[2] => 'G,N'
	[3] => 'R,U'
)

Edward

-- 
PHP General Mailing List (http://www.php.net/)
To unsubscribe, visit: http://www.php.net/unsub.php


[Index of Archives]     [PHP Home]     [Apache Users]     [PHP on Windows]     [Kernel Newbies]     [PHP Install]     [PHP Classes]     [Pear]     [Postgresql]     [Postgresql PHP]     [PHP on Windows]     [PHP Database Programming]     [PHP SOAP]

  Powered by Linux