Jump to content

how to filter an array?


shedokan

Recommended Posts

I have this array:

<?php
$array=array(array(1,2,3,4),array(1,2,4,3),array(6,2,4,4),array(6,4,2,4),array(6,5,4,1),array(6,4,1,5)
);
?>

is there a way to filter so there won't be arrays that are almost the same like:

1,2,3,4 is equal to 1,2,4,3

6,2,4,4 is equal to 6,4,2,4

6,5,4,1 is equal to 6,4,1,5

 

please, thanks.

 

Link to comment
Share on other sites

this may not be perfect:

 

$array=array(array(1,2,3,4),array(1,2,4,3),array(6,2,4,4),array(6,4,2,4),array(6,5,4,1),array(6,4,1,5));

for ($i=0;$i<count($array);$i++) {
sort($array[$i]);
}

$current_array = "";
$unique_arrays = array();
foreach($array AS $an_array) {
$last_different = FALSE;

if ($current_array === "") {
	$current_array = $an_array;
} else {	
	// Compare an_array with current_array
	for($j=0;$j<count($current_array);$j++) {
		if ($current_array[$j] != $an_array[$j]) {			
			$unique_arrays[] = $current_array;
			$current_array = $an_array;
			$last_different = TRUE;
			break;
		}
	}
}
}

// Don't forget the last an_array
$unique_arrays[] = $an_array;

echo"<P>UNIQUE:</P>";

print_r($unique_arrays);

Link to comment
Share on other sites

try

<?php
$array = array(
            array(1,2,3,4),
            array(1,2,4,3),
            array(6,2,4,4),
            array(6,4,2,4),
            array(6,5,4,1),
            array(6,4,1,5)
);

$x = array();
$filtered = array();

foreach ($array as $a)
{
    if (array_intersect($a, $x) != $a)
    $filtered[] = $a;
    $x = $a;
}

// result
echo '<pre>', print_r($filtered, true), '</pre>';
?>

Link to comment
Share on other sites

try

<?php
$array = array(
            array(1,2,3,4),
            array(1,2,4,3),
            array(6,2,4,4),
            array(6,4,2,4),
            array(6,5,4,1),
            array(6,4,1,5)
);

$x = array();
$filtered = array();

foreach ($array as $a)
{
    if (array_intersect($a, $x) != $a)
    $filtered[] = $a;
    $x = $a;
}

// result
echo '<pre>', print_r($filtered, true), '</pre>';
?>

 

I was writing an entire script and this came up. Lol

Link to comment
Share on other sites

It does depend on the array being conveniently sequenced. If they are jumbled then would require sorting first eg

<?php
$array = array(
            array(1,2,3,4),
            array(6,5,4,1),
            array(6,2,4,4),
            array(6,4,2,4),
            array(1,2,4,3),
            array(6,4,1,5)
);

foreach ($array as $k=>$a)
{
    sort($a);
    $array[$k] = $a;
}

array_multisort($array);

$x = array();
$filtered = array();

foreach ($array as $a)
{
    if (array_intersect($a, $x) != $a)
    $filtered[] = $a;
    $x = $a;
}

echo '<pre>', print_r($filtered, true), '</pre>';
?>

Link to comment
Share on other sites

help please, it doesn't filters all

here's my code:

<?php
$code=<<<END
3+2+4+7
3+1+1+5
3+1+5+1
3+2+7+4
3+4+2+7
END;
$code = explode("
", $code);
$count=count($code);
for($i=0;$i<=$count;$i++)
$code[$i] = explode("+", $code[$i]);
foreach ($code as $k=>$a)

{
    sort($a);
    $code[$k] = $a;
}

for($i=1;$i<=2;$i++){

$x = array();
$filtered = array();

foreach ($code as $a)
{
    if (array_intersect($a, $x) != $a)
    $filtered[] = $a;
    $x = $a;
}
$code=$filtered;
}
$count=count($code)-1;
for($i=0;$i<=$count;$i++){
$d=$code[$i][0];
$c=$code[$i][1];
$b=$code[$i][2];
$a=$code[$i][3];
if($a != '' || $b != '' || $c != '' || $d != '')
	$final .=$a.'+'.$b.'+'.$c.'+'.$d.'='."\n";
}
echo $final;
?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.