PHP - 将数组值组合成唯一的两对
PHP - Combine array values to unique pairs of two
我正在尝试转换以下数组
Array
(
[304] => Array
(
[0] => 102
[1] => 177
[2] => 132
[3] => 223
)
[302] => Array
(
[0] => 132
[1] => 96
)
)
进入以下:
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
)
)
所以我得到了所有可能的对,没有任何重复!
我尝试了很多东西,比如使用 if 语句的循环中的循环,但我不知道真正从哪里开始...
一个例子是:
$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;
}
}
}
其中 $arr 是给定的数组
所以我的问题是:实现此目标的最有效方法是什么?
提前致谢!!
-----编辑-----
感谢 chba!!
我只需要稍微编辑一下语法就可以得到它 运行 但是逻辑很有魅力!!
我的最终结果是:
// 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++;
}
}
<?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);
我正在尝试转换以下数组
Array
(
[304] => Array
(
[0] => 102
[1] => 177
[2] => 132
[3] => 223
)
[302] => Array
(
[0] => 132
[1] => 96
)
)
进入以下:
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
)
)
所以我得到了所有可能的对,没有任何重复!
我尝试了很多东西,比如使用 if 语句的循环中的循环,但我不知道真正从哪里开始...
一个例子是:
$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;
}
}
}
其中 $arr 是给定的数组
所以我的问题是:实现此目标的最有效方法是什么?
提前致谢!!
-----编辑-----
感谢 chba!!
我只需要稍微编辑一下语法就可以得到它 运行 但是逻辑很有魅力!!
我的最终结果是:
// 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++;
}
}
<?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);