Re: Recursive function for array task?

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

 



What you want is strange...

but this is the solution

<?
$left = Array
(
    "B" => array('X'),
    "C" => array('Q','K'),
    "D" => array('M'),
    "F" => array('V'),
    "G" => array('N'),
    "I" => array('X','M'),
    "K" => array('C'),
    "M" => array('I','V'),
    "Q" => array('C'),
    "R" => array('U'),
    "V" => array('M'),
    "X" => array('I'),
);

$right = array();
foreach($left as $key1 => $values)
    foreach($values as $key2)
        $right[$key2][] = $key1;

$pairs = array();
foreach($left as $key1 => $values)
    foreach($values as $key2)
        if (in_array($key1, $right[$key2]))
            $pairs[] = array(min($key1, $key2), max($key1, $key2));

$groups = array();
foreach($pairs as $keys)
{
    list($key1, $key2) = $keys;
    $foundGroup = false;
    foreach($groups as &$group)
    {
        if (in_array($key1, $group))
        {
            $group[] = $key2;
            $foundGroup = true;
            break;
        }
        else if (in_array($key2, $group))
        {
            $group[] = $key1;
            $foundGroup = true;
            break;
        }
    }
    if ($foundGroup == false)
        $groups[] = $keys;
}

foreach($groups as &$group)
    $group = array_unique($group);

print_r($groups);
?>

Am 27.04.2007, 11:13 Uhr, schrieb Stewart Macdonald <s.macdonald@xxxxxxxxxxxxx>:

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


--
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