...

View Full Version : Filtering array problem



MarioPro
12-29-2005, 12:48 PM
I've been looping arround this resultset trying to find a way to filter this and nothing. Anyone available to give me a hand here? Thanks in advance.

Here's what's up:
These are the results I have on $foundStyles but the ones in red shouldn't go to the query. Is there a way to filter these results?

KEY: 0 & VALUE: 3488
KEY: 1 & VALUE: 3485
KEY: 2 & VALUE: 2860
KEY: 3 & VALUE: 3482
KEY: 4 & VALUE: 3
KEY: 5 & VALUE: 0
KEY: 6 & VALUE: 1
KEY: 7 & VALUE: 2
KEY: 8 & VALUE: 3
KEY: 9 & VALUE: 4

In fact, I think that my problem comes a little from above where I have the following code to obtain ONLY the values in the array that have more than one occurence.




// The array $foundStyles would seem something like this
$foundstyles=array(0=>3488, 1=>3485, 2=>3488, 3=>2562, 4=>3485, 5=>1785, 6=>2232);

// I just want the 3488 and 3485 since these are the only ones that are repeated
// and what I need is to make the array $foundstyles to become like this
// array(0=>3488, 1=>3485) and for that I made the code below

// GET ONLY THE KEYS DUPLICATES FOR NEXT QUERY
if($rows>0){ // From mysql_query
$foundStyles=array_count_values($foundStyles);
}
foreach($foundStyles as $key=>$value){
if($value>=$total_styles){
$foundStyles[]=$key;
}
}



Humm....

schleppel
12-29-2005, 01:36 PM
I'm slightly confused about what you want, if you want to find the duplicates in an array, you could use this:

function duplicates($array)
{
$dupes = array();
$num = array_count_values($array);
foreach($num as $k => $v)
{
if($v >= 2)
{
$dupes[] = $k;
}
}
return $dupes;
}

// test it:
$test = array(1,1,2,3,4,4,5,5,5,6,7,7,7,7);
header('Content-Type: text/plain');
print_r(duplicates($test));
/* Outputs:
Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 7
)*/

The problem from your original code, might be that you are appending to the array that already had the array_count_values() in it, and where does $total_styles come from?

Edit: spelling.

MarioPro
12-29-2005, 01:44 PM
Thanks schleppel,

I was browsing and browsing the php website trying to find examples podted by users and as I was just getting to some place you posted this.
Yes, this is what I need, because I'm quering a database in a foreach loop that will return an array with values that may or not be duplicated. Since I just want the ones that are duplicates (the oposite from array_unique) this function will do the job (I hope but believe it will).
Let me test it and I'll post here the feedback. Meanwhile, thanks for your help.

Opss. Answering your question about $total_styles. $total_styles is the variable containing the total $styles[] array for wich I have to loop the db query.




$total_styles=count($styleID);
foreach($styleID as $k=>$v){
// SEARCH
$query="SELECT * FROM pstyles_rel ";
$query.=" WHERE style_ID='$v' ";
$result=mysql_query($query) or die("Can't select styles: ".mysql_error());
$rows=mysql_num_rows($result);
if($rows>0){
while($fields=mysql_fetch_array($result)){
$foundStyles[]=$fields["p_ID"]; // the 3488 and so on values I need
}
}
}

MarioPro
12-29-2005, 02:01 PM
Oh, I forgot a little issue, is that I want in fact only the one that are duplicate but if the array contains more than one value, otherwise I will miss a result.

in this I would apply your function:
$test = array(1,1,2,3,4,4,5,5,5,6,7,7,7,7);
header('Content-Type: text/plain');
print_r(duplicates($test));
/* Outputs:
Array
(
[0] => 1
[1] => 4
[2] => 5
[3] => 7
)

but in this I would miss it
$test = array(7);

schleppel
12-29-2005, 02:14 PM
So if the array only has one value, you don't want to test for duplicates?


if(count($array) > 1)
{
// test for duplicates
}
else
{
// don't test for duplicates
}

Or are you asking for something else?

MarioPro
12-29-2005, 02:21 PM
Forgive-me.

I only whant to check for duplicates if the $total_styles > 1, this is to say that if $total_styles==1 than I will only loop 1 time on the query and thus no duplicates will be found.

So I think I'm right if I do it like




if($total_styles>1 && count($array)>$total_styles)
{
// test for duplicates
}
else
{
// don't test for duplicates
}



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum