Page 1 of 1

sanity is being lost...array_search problems

Posted: Tue Jan 19, 2010 3:44 pm
by Monotoko
Hi guys, was just wondering if anyone would mind helping me, i have been over this millions of times and still cannot see anything wrong with it...but its not working, i appologise for lack of PHP tags, but its just the snippits that are going wrong.

Iv been over this code for the past hour and a half, it all seems right, yet its not working!

I have a function in my program known as splitcsv:

Code: Select all

function splitcsv($string, $separator=",")
{
    $elements = explode($separator, $string);
    for ($i = 0; $i < count($elements); $i++) {
        $nquotes = substr_count($elements[$i], '"');
        if ($nquotes %2 == 1) {
            for ($j = $i+1; $j < count($elements); $j++) {
                if (substr_count($elements[$j], '"') %2 == 1) { // Look for an odd-number of quotes
                    // Put the quoted string's pieces back together again
                    array_splice($elements, $i, $j-$i+1,
                        implode($separator, array_slice($elements, $i, $j-$i+1)));
                    break;
                }
            }
        }
A call to this function is made to create an array in one of my php files, $searched is defined by what the user searches for:

Code: Select all

$searchfile = splitcsv(file_get_contents("searches.csv"));
// This gets rid of any annoying spaces in the search
array_walk($searchfile, 'trim_value');
// This performs the search itself
$searchfilefor = array_search($searched,$searchfile);
It then goes through a series of If statements,

Code: Select all

if ($searchfilefor == "")
{
//This will execute if the searched file isnt found, this works perfectly
$fh = fopen("searches.csv", "a");
fwrite($fh, $searched);
fclose($fh);
}
 
if ($searchfilefor != "")
{
//This however...will not echo, even when $searchfilefor has a value???
echo "Hello World!<br>";
}
It appears that when $searchfilefor is set, none of the conditions are true..??

Code: Select all

 
//When the value searched is in the searchfile, it will display $searchfilefor number here, so its definatly set
var_dump($searchfile);
echo "<br><br>";
echo "I am trying to find: '$searched' <br> It is located at:".$searchfilefor;
For example:
I am trying to find: 'jj'
It is located at:7

Re: sanity is being lost...array_search problems

Posted: Tue Jan 19, 2010 5:06 pm
by Jade
Have you tried:

Code: Select all

 
if (!isset($searchfilefor))
{
   $fh = fopen("searches.csv", "a");
   fwrite($fh, $searched);
   fclose($fh);
}
else
     echo "Hello World!<br>";
 
You'll never enter into the second part of the if statement because you'll get a blank array. Try using this function, it'll show you the entire contents of the variable's structure:

Code: Select all

 
print_r($searched);