0

我在将多维数组缩减为普通数组时遇到了一些问题。

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

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
    )

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

我正在尝试解决这个问题一个多星期,但我没有让它工作。我知道这应该很容易......但无论如何 - 我不明白: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]));
        }

    }

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

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

4

5 回答 5

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
}

原答案

这个问题问得不好,但我会尝试回答我认为的问题。

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

$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
}
于 2012-11-06T20:59:47.173 回答
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);
于 2012-11-06T21:13:19.777 回答
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);

演示 它使用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) }
          }
于 2012-11-06T21:18:09.897 回答
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
        )

)
于 2012-11-06T21:58:39.693 回答
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),
)));
于 2012-11-06T22:08:18.023 回答