0

I'm just wondering is it possible to have a csv file (with many values) and scan the file for a specific value with php?

Ideally if this is possible, Id also like to get the index of that value in the file (i.e what row number the value falls on).

I'm quiet new to php so I'm not sure where to start. All help is greatly appreciated.

2

1 Answer 1

1

Sure,

Not tested:

<?php
$search = 'mysearchstring';
$row = 1;
if (($handle = fopen("test.csv", "r")) !== FALSE) 
{
    while (($data = fgetcsv($handle, 1000, ",")) !== FALSE) 
    {
        $num = count($data);
        $row++;
        for ($c=0; $c < $num; $c++) 
        {
            if ($data[$c] == $search)
            {
               echo "Found in row $row in column {$c+1} <br />\n";
            }
        }
    }
    fclose($handle);
}
?>
Sign up to request clarification or add additional context in comments.

5 Comments

Thanks a million! @Mark van Herpen, would it work the same if I was to search the file by row number?
Sure, just check $row for it's value, so if ($row == 5) or something
If I was to search another file for the row number that Ive just gotten, how would I even get the value of that row though? I appreciate your help !! :) @Mark van Herpen
OK, I guess that's outside the scope of this question.. You should store the value in a variable, open the second file and go through it the same way until the rownumber of this second file is the same as the first one.
Thanks a mill! you've been a great help!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.