KddC
KddC

Reputation: 3013

Making values unique in multidimensional array

I have some Problems reducing a multidimensional array into a normal one.

I have an input array like this:

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
    )

So I have a multidimensional array with some overlaps in the values (eg 17,99 and 17,121) Now I want to have an output like this:

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

I want to save, which articles are the same in my database this way. The output array shpuld still be a multidimesional array, but every number on the second level should be unique in the array.

I'm trying to solve this for more than a week now, but I dont get it to work. I know it should be easy...but anyway - I dont get it :D

This is what i got so far:

$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]));
        }

    }

Did you maybe already solve problem like this or is there a much easier way? Maybe I just think too complicated (It's not my first try, but this code was the last try)

Any help would be appreciated. Thanks a lot

Upvotes: 0

Views: 218

Answers (5)

iMoses
iMoses

Reputation: 4348

(Code examples: http://codepad.org/rJNNq5Vd)

I truly believe I understand you and if this is the case here is what you're looking for:

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

Which will return:

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)
  }
}

For:

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

And:

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

For:

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

Upvotes: 0

Salman
Salman

Reputation: 3726

I think I get your problem. Let me have a crack at it.

$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 ] );
    }
}

Output:

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

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

)

Upvotes: 0

paquettg
paquettg

Reputation: 1374

Here is my revised code given your comment and a DEMO of it working as expected. ( 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

The question is badly asked, but I'll attempt to answer what I believe the question is.

You want to gather all the pairs of arrays that have the same first value in the pair correct?

$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
}

Upvotes: 1

Teena Thomas
Teena Thomas

Reputation: 5239

Try:

$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 It uses array_unique and array_chunk.

Output:

 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) }
          }

Upvotes: 0

dark_gf
dark_gf

Reputation: 726

Here is solution for common task.

$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);

Upvotes: 0

Related Questions