PHP - 同一数组中的数组

时间:2016-06-03 20:35:55

标签: php arrays foreach

如何在foreach循环中包含相同的数组,并在另一个数组中删除相同的值。我的PHP技能不强,我想在这里寻求专家的帮助。请参阅下面的方案。

$array = array("group1","group2","group3","group4");
//Result should be
group1 = group2,group3,group4
group2 = group1,group3,group4
group3 = group1,group2,group4
group4 = group1,group2,group3

我只在:

 $array = array('group1','group2','group3','group4');


foreach($array as $value){
  echo '<br>'.$prodfilters.'= <br>';
  foreach($arrayFilter as $xx){
    echo $xx.'<br>';
  }
}
//Result
group1 = group1,group2,group3,group4
group2 = group1,group2,group3,group4
group3 = group1,group2,group3,group4
group4 = group1,group2,group3,group4
//basically repeating itself each value from the outer foreach loop instead of removing the same value from the inner foreach loop.

希望你能在这里帮助新手。提前谢谢。

4 个答案:

答案 0 :(得分:2)

以下是使用array_walkarray_diff函数的简短解决方案:

$array = ["group1","group2","group3","group4"];
$result = [];

array_walk($array, function($v) use(&$result, $array){
    $result[$v] = array_diff($array, [$v]);   // it can be imploded into a string if needed
});

print_r($result);

输出:

Array
(
    [group1] => Array
        (
            [1] => group2
            [2] => group3
            [3] => group4
        )

    [group2] => Array
        (
            [0] => group1
            [2] => group3
            [3] => group4
        )

    [group3] => Array
        (
            [0] => group1
            [1] => group2
            [3] => group4
        )

    [group4] => Array
        (
            [0] => group1
            [1] => group2
            [2] => group3
        )
)

现在,您可以通过将“名称”指定为数组键来访问每个项目,例如$result['group3']

答案 1 :(得分:1)

<?php
$array = ["group1","group2","group3","group4"];
$new = [];
foreach ($array as $key1) {
    $new[$key1] = [];
    foreach ($array as $key2) {
        if ($key1 !== $key2) {
            $new[$key1][] = $key2;
        }
    }
}
print_r($new);

答案 2 :(得分:0)

我并不完全想要实现你想要的。 这是一个迭代的建议:

foreach($array as $value){
     $result.=$value.' = ';
     foreach($array as $value2){
         if($value2 != $value){
              $result.=$value2.',';
         }
     }
     $result.=rtrim($result,',').'\n'; //replace \n with <br/> for html linefeed
}
echo $result;

答案 3 :(得分:0)

这是一个有趣的解决方案,因为它可以使用:

还有一些有用的数组函数:

这将提供单独的数组,每个数组都以主数组中各自的值命名:

<?php
$array = array("group1","group2","group3","group4");

foreach ($array as $element)
  $$element = array_values(array_filter($array,function($item) use ($element){return $item !== $element; }));

现在,您可以按索引名称使用各个数组:

print_r($group1);

显示:

Array
(
    [0] => group2
    [1] => group3
    [2] => group4
)

您可以使用get_defined_vars

查看环境中的所有变量
print_r(get_defined_vars());

在源代码中显示:

[group1] => Array
    (
        [0] => group2
        [1] => group3
        [2] => group4
    )

[group2] => Array
    (
        [0] => group1
        [1] => group3
        [2] => group4
    )

[group3] => Array
    (
        [0] => group1
        [1] => group2
        [2] => group4
    )

[group4] => Array
    (
        [0] => group1
        [1] => group2
        [2] => group3
    )