开发者

Combine pairs to groups [PHP / Arrays]

开发者 https://www.devze.com 2022-12-13 08:50 出处:网络
I have pairs of items in an PHP array. Example: <?php $elements = array( \'tiger\'=>\'lion\', \'car\'=>\'bike\',

I have pairs of items in an PHP array. Example:

<?php
$elements = array(
    'tiger'=>'lion',
    'car'=>'bike',
    'lion'=>'zoo',
    'truck'=>'plane'
);
?>

Now I want to combine these items so that all items which are connected in any way go to one group. Continuation of the example above:

<?php
$gro开发者_开发百科ups = array(
    0=>array('tiger', 'lion', 'zoo'),
    1=>array('car', 'bike'),
    2=>array('truck', 'plane'
);
?>

Is this understandable? How could I achieve this?

I'm looking for a function which does this.


<?php

$elements = array(
    'tiger' => 'lion',
    'car' => 'bike',
    'lion' => 'zoo',
    'truck' => 'plane'
);

$groups = array();

foreach ($elements as $key => $val) {
    $appended = false;
    foreach ($groups as &$group) {
        if ($group[0] == $key) {
            array_unshift($group, $val);
            $appended = true;
            break;
        }
    }
    if (!$appended) {
        $groups[] = array($val, $key);
    }
}

var_dump($groups);

Gives:

array(3) {
  [0]=>
  array(3) {
    [0]=>
    string(3) "zoo"
    [1]=>
    string(4) "lion"
    [2]=>
    string(5) "tiger"
  }
  [1]=>
  &array(2) {
    [0]=>
    string(4) "bike"
    [1]=>
    string(3) "car"
  }
  [2]=>
  array(2) {
    [0]=>
    string(5) "plane"
    [1]=>
    string(5) "truck"
  }
}


Here's an O(n) solution:

$elements = array(
    'tiger' => 'lion',
    'car'   => 'bike',
    'lion'  => 'zoo',
    'truck' => 'plane'
);
$groups = array();
$sub    = array();
$ignore = array();

foreach ( $elements as $key=>$value ) {
    if ( isset($ignore[$key]) ) {
        continue;
    }

    $sub = array($key, $value);

    if ( isset($elements[$value]) ) {
        $ignore[$value] = 1;
        $sub[]          = $elements[$value];
    }

    $groups[] = $sub;
}

print_r($groups);

Result:

Array
(
    [0] => Array
        (
            [0] => tiger
            [1] => lion
            [2] => zoo
        )

    [1] => Array
        (
            [0] => car
            [1] => bike
        )

    [2] => Array
        (
            [0] => truck
            [1] => plane
        )

)


The idea is simple:

  1. Create a new array to hold your groups
  2. Loop over the item array
  3. Check if the group for the item exists in the group array - if it does not, create it
  4. Put item in group
0

精彩评论

暂无评论...
验证码 换一张
取 消