在多维数组中使值唯一

时间:2012-11-06 20:47:16

标签: php arrays multidimensional-array

我有一些问题将多维数组简化为正常数组。

我有一个像这样的输入数组:

Array
(
[0] => Array (
        [0] => 17
        [1] => 99
    )
[1] => Array (
        [0] => 17
        [1] => 121
    )
[2] => Array (
        [0] => 99
        [1] => 77
    )
[3] => Array (
        [0] => 45
        [1] => 51
    )
[4] => Array (
        [0] => 45
        [1] => 131
    )

所以我有一个多维数组,在值中有一些重叠(例如17,99和17,121) 现在我希望有这样的输出:

Array
(
[0] => Array (
        [0] => 17
        [1] => 99
        [2] => 121
        [3] => 77
    )
[2] => Array (
        [0] => 45
        [1] => 51
        [3] => 131
    )

我想保存,这样在我的数据库中哪些文章是相同的。输出数组shpuld仍然是一个多维数组,但第二级的每个数字在数组中都应该是唯一的。

我现在试图解决这个问题一个多星期了,但是我没有让它发挥作用。我知道它应该很容易......但无论如何 - 我不明白:D

这是我到目前为止所得到的:

$parity_sorted = array();
    foreach($arr as $key => $a){
        if(count($parity_sorted) > 0){
        foreach($parity_sorted as $key2 => $arr_new){
            if(in_array($a[0], $arr_new) || in_array($a[1], $arr_new)){
                if(!in_array($a[0], $arr_new)){array_push($parity_sorted[$key2], $a[0]);}
                if(!in_array($a[1], $arr_new)){array_push($parity_sorted[$key2], $a[1]);}
            } else {
                array_push($parity_sorted, array($a[0],$a[1]));
            }
        }
        } else {
            array_push($parity_sorted, array($a[0],$a[1]));
        }

    }

您是否已经解决过这样的问题或者有更简单的方法吗?也许我只是觉得太复杂了(这不是我的第一次尝试,但这段代码是最后一次尝试)

任何帮助将不胜感激。非常感谢

5 个答案:

答案 0 :(得分:1)

这是我修改后的代码,给出了您的评论,并且其演示文稿按预期工作。 (http://codepad.org/CiukXctS

<?php

$tmp = array();
foreach($array as $value)
{
    // just for claraty, let's set the variables
    $val1 = $value[0];
    $val2 = $value[1];
    $found = false;
    foreach($tmp as &$v)
    {
        // check all existing tmp for one that matches
        if(in_array($val1, $v) OR in_array($val2, $v))
        {
            // this one found a match, add and stop
            $v[] = $val1;
            $v[] = $val2;
            // set the flag
            $found = true;
            break;
        }
    }
    unset($v);

    // check if this set was found  
    if( ! $found)
    {
        // this variable is new, set both
        $tmp[] = array(
                $val1,
                $val2,
                );
    }
}

// go trough it all again to ensure uniqueness
$array = array();
foreach($tmp as $value)
{
    $array[] = array_unique($value); // this will eliminate the duplicates from $val2
}

ORIGIN ANSWER

问题很严重,但我会尝试回答我认为的问题。

您想收集对中具有相同第一个值的所有数组对吗?

$tmp = array();
for($array as $value)
{
    // just for claraty, let's set the variables
    $val1 = $value[0];
    $val2 = $value[1];

    if(isset($tmp[$val1])) // we already found it
    {
        $tmp[$val1][] = $val2; // only set the second one
    }
    else
    {
        // this variable is new, set both
        $tmp[$val1] = array(
            $val1,
            $val2,
        );
    }
}
// go trough it all again to change the index to being 0-1-2-3-4....
$array = array();
foreach($tmp as $value)
{
    $array[] = array_unique($value); // this will eliminate the duplicates from $val2
}

答案 1 :(得分:0)

这是常见任务的解决方案。

$data = array(array(17,99), array(17,121), array(99,77), array(45,51), array(45,131));
$result = array();
foreach ($data as $innner_array) {
    $intersect_array = array();
    foreach ($result as $key => $result_inner_array) {
        $intersect_array = array_intersect($innner_array, $result_inner_array);
    }
    if (empty($intersect_array)) {
        $result[] = $innner_array;
    } else {
        $result[$key] = array_unique(array_merge($innner_array,     $result_inner_array));
    }

}
var_dump($result);

答案 2 :(得分:0)

尝试:

$arr = array(array(17,99), 
            array(17,121),
            array(99,77),
            array(45, 51),
            array(45, 131)
      );

 foreach($arr as $v)
  foreach($v as $m)
    $new_arr[] = $m;

  $array = array_chunk(array_unique($new_arr), 4);
  var_dump($array);

Demo 它使用array_uniquearray_chunk

<强>输出:

 array(2) { [0]=>array(4) { [0]=> int(17) [1]=>int(99)
            [2]=>int(121) [3]=> int(77) }
            [1]=> array(3) { [0]=> int(45) [1]=>int(51)
            [2]=>int(131) }
          }

答案 3 :(得分:0)

我想我得到了你的问题。让我对此有所了解。

$firstElems = array();
$secondElems = array();
foreach ( $arr as $v ) {
    $firstElems[ $v[0] ] = array( $v[0] );
}
foreach ( $arr as $v ) {
    $secondElems[ $v[1] ] = $v[0];
}

foreach ( $arr as $v ) {
    if ( isset( $secondElems[ $v[0] ] ) ) {
        array_push( $firstElems[ $secondElems[ $v[0] ] ], $v[1] );
    }
    else {
        array_push( $firstElems[ $v[0] ], $v[1] );
    }
}

foreach ( $firstElems as $k => $v ) {
    if ( isset( $secondElems[ $k ] ) ) {
        unset( $firstElems[ $k ] );
    }
}

输出:

Array
(
    [17] => Array
        (
            [0] => 17
            [1] => 99
            [2] => 121
            [3] => 77
        )

    [45] => Array
        (
            [0] => 45
            [1] => 51
            [2] => 131
        )

)

答案 4 :(得分:0)

(代码示例:http://codepad.org/rJNNq5Vd

我真的相信我了解你,如果是这种情况,那么你正在寻找:

function arrangeArray($array) {
    $newArray = array(array_shift($array));

    for ($x = 0; $x < count($newArray); $x++) {
        if (!is_array($newArray[$x])) {
            unset($newArray[$x]);
            return $newArray;
        }
        for ($i = 0; $i < count($newArray[$x]); $i++) {
            foreach ($array as $key => $inArray) {
                if (in_array($newArray[$x][$i], $inArray)) {
                    $newArray[$x] = array_unique(array_merge($newArray[$x], $inArray));
                    unset($array[$key]);
                }
            }
        }
        $newArray[] = array_shift($array);
    }
}

将返回:

array(2) {
  [0]=>
  array(4) {
    [0]=>
    int(17)
    [1]=>
    int(99)
    [2]=>
    int(121)
    [4]=>
    int(77)
  }
  [1]=>
  array(3) {
    [0]=>
    int(45)
    [1]=>
    int(51)
    [3]=>
    int(131)
  }
}

有关:

var_dump(arrangeArray(array(
    array(17,99),
    array(17,121),
    array(99,77),
    array(45, 51),
    array(45, 131),
)));

array(1) {
  [0]=>
  array(6) {
    [0]=>
    int(17)
    [1]=>
    int(99)
    [2]=>
    int(121)
    [3]=>
    int(45)
    [4]=>
    int(77)
    [6]=>
    int(51)
  }
}

有关:

var_dump(arrangeArray(array(
    array(17,99),
    array(17,121),
    array(99,77),
    array(45, 51),
    array(45, 17),
)));