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
https://forums.phpfreaks.com/topic/90139-how-to-filter-an-array/
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);

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>';
?>

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

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>';
?>

And that will always be the case unless you try.

 

Read the manual, play around with them, experiment. See what they do.

 

Did you think everyone else was born knowing how they all work?

 

If you get really stuck, post back.

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;
?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.