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++;
}
}