...

View Full Version : How do I check the contents of an array for my string?



jeddi
01-25-2010, 06:36 AM
Hi,

I a string that is coming from my database table say $needle.
If te needle is not in my array, then I want to add it to my array.

If it IS in my array then so long as it is in only twice, then I still
want to add it to my array (so three times will be the maximum)

In order to check to see is if $needle is in my $haystack array, do I
need to loop through the array with strpos() or is there a quicker method ?

There are many needles in the table so I start by looping through
the select result.

This is the schematic of what I am trying to do...




$haystack = array();

while( $row = mysql_fetch_assoc($result)) {
$needle = $row['data'];

$num = no. of times $needle is in $haystack // $haystack is an array

if ($num < 3 ) {
$$haystack[] = $needle; // hopfully this adds the needle
}

} // end while. Get next needle.



Does anyone know how do I do this bit:

$num = no. of times $needle is in $haystack


thanks

Dormilich
01-25-2010, 09:08 AM
try array_keys() (http://php.net/array-keys).

jeddi
01-25-2010, 09:47 AM
OK,
Looked at that but it looks as if I need:
array_values($array)

using this - I guess I would still need to step through the array
wouldn't I ?

How about if I implode() my array and then do a substr_count()
then I could do it without looping couldn't I ?

At a guess the array may contain 1000 values at the most.

So would this make sense:


$haystack = array();
$haystack = implode(",", $haystack);

while( $row = mysql_fetch_assoc($result)) {
$needle = $row['data'];

$num = substr_count($haystack, $needle);

if ($num < 3 ) {
$haystack = $haystack.",".$needle; // hopefully this adds the needle
}

} // end while. Get next needle.


In fact I don't even need the array until after I have
added all built up the string so I could do:



$haystack = '';

while( $row = mysql_fetch_assoc($result)) {
$needle = $row['data'];

$num = substr_count($haystack, $needle);

if ($num < 3 ) {
$haystack = $haystack.",".$needle; // hopefully this adds the needle
}

} // end while. Get next needle.

$my_array = explode( ',' , $haystack);


That makes more sense - anything wrong with doingit this way ?

Dormilich
01-25-2010, 10:06 AM
OK,
Looked at that but it looks as if I need:
array_values($array)

maybe you didnít read that:

search_value

If specified, then only keys containing these values are returned.

that is, a count of the returned array gives you the number of occurences. should be enough for a simple match, otherwise you may want to use something like array_filter().

jeddi
01-25-2010, 10:29 AM
No didn't see that
I went to this page:
array-values (http://php.net/manual/en/function.array-values.php)

Anyway - I guess my solution is OK ?

After using $haystack = ltrim($haystack, ',');
to clean it up I exploded it into an array

seems to work fine.

BTW - You couldn't help me out by looking at my other
problem in the thread titled:
Why is my script writing 500,000 records ?

Thanks for all your help.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum