-1

I am trying to transform the following array

    Array
    (
        [304] => Array
            (
                [0] => 102
                [1] => 177
                [2] => 132
                [3] => 223
            )
        [302] => Array
            (
                [0] => 132
                [1] => 96
            )
    )

into the following:

    Array
    (
        [0] => Array
            (
                ["source"] => 102
                ["target"] => 177
            )
        [1] => Array
            (
                ["source"] => 102
                ["target"] => 132
            )
        [2] => Array
            (
                ["source"] => 102
                ["target"] => 223
            )
        [3] => Array
            (
                ["source"] => 177
                ["target"] => 132
            )
        [4] => Array
            (
                ["source"] => 177
                ["target"] => 223
            )
        [4] => Array
            (
                ["source"] => 132
                ["target"] => 223
            )
        // only two values, so just one pair
        [5] => Array
            (
                ["source"] => 132
                ["target"] => 96
            )
    )

so that i got all possible pairs without any duplicates!

I was trying a lot of things, like loops in loops with if statements but i have no idea, where realy to start...
an example is:

    $new_links = array();
     foreach($arr as $arr_new){
      foreach($arr_new as $key => $value){
        if($key == 0){
          $source=$value;
        }else{
          $new_links[$key]["source"]=$source;
          $new_links[$key]["target"]=$value;
        }
      }
     }

where $arr is the given array

So my question: what would be the most efficient way to achieve this?

Thanks in advance!!

----- edit -----

thanks to chba!!

i just had to edit the syntax a bit to get it running but the logic works like a charm!!

my final result is:

    // the given array is $arr
    $result = array();

    foreach ($arr as $group)
    {
        $lastIdx = count($group) - 1;
        $startIdx = 1;

        foreach ($group as $member)
        {
            for ($pos = $startIdx; $pos <= $lastIdx; $pos++)
            {
                $result[] = array(
                    'source' => $member,
                    'target' => $group[$pos]
                );
            }

            $startIdx++;
        }
    }
Community
  • 1
  • 1
FalcoB
  • 1,333
  • 10
  • 25

1 Answers1

3
<?php

$input = [[102,177,132,223],[132,96]];
$result = [];

// for each group of elements in input array
foreach ($input as $group)
{
    // set the first target element of the group to be
    // second element
    $nextTargetIdx = 1;

    // determine last target index beforehand
    // so that value gets computed only once per group
    $lastTargetIdx = count($group) - 1;

    // then, take each element of that group as source
    foreach ($group as $source)
    {
        // and 
        for // every next element
        (
            $targetIdx = $nextTargetIdx;
            $targetIdx <= $lastTargetIdx;
            $targetIdx++
        )
        {
            // add new result entry
            $result[] = [
                // with current source
                'source' => $source,
                // and target
                'target' => $group[$targetIdx]
            ];
        }

        // then, when all targets for current source are found
        // increase next target index so that it follows next source element
        $nextTargetIdx++;
    }
}

var_dump($result);
featherbits
  • 790
  • 6
  • 24
  • @mickmackusa Not sure what to explain.. so added comments. – featherbits Sep 26 '18 at 23:25
  • Thank you. Adding explanation to solutions does many great things. 1. Improves the culture (expected standard) of answers in the future. 2. Does a great job of educating/empowering the OP and future researchers. 3. Makes using existing pages as resources ti close new duplicate question much more helpful. 4. Improves the likelihood that you will earn upvotes. 5. Makes StackOverflow an optimal resource for all. ...Once again, thank you for taking the time. Please always and forever answer with the intent to educate. – mickmackusa Sep 26 '18 at 23:40