I’m reading data from text file and then reading data from csv while reading from csv m comparing txt daya to csv data if it matches then printing found. but there is some issue in if condition so i’m not able to compare.
when i’m using any value(string) from SIS.txt at the place of$b[$j] it’ll work for that string but if i’m using $b[$j] it is not working. $b is the data from txt file and $line_of_text[2] data from 3rd column of csv.
I should think that one of the problems you will see is that once you try to compare against the second element of $b, your inner while() loop won’t execute. The layout of your code is:
// read the sis.txt file into an array
// open the csv file
// for each element in $b:
// loop around every entry in the csv file until EOF
// compare the two entries, display found or not found
// end loop
// loop back for next element in $b
The problem is that as soon as you’ve run through that while loop the first time, when you go to do the same for the second element, your CSV file is already at EOF and the while won’t run. You need to either reset the file pointer in the CSV file, or just change the layout of the code so it opens the file inside your for() loop.
OK, here’s where adding a var_dump gives you the clue. If you var_dump your $b array once you’ve loaded it, you will see that each element is a bit longer than it appears to be, and the closing quote appears on the next line of the screen. This shows there’s perhaps a CR, or a LF, or some non-printable character in that file (perhaps because there is only the single string on each line), which means that those strings are not the same as the strings extracted from the CSV file that do not have CR or LF on the end.
So I made this change:
$b[$i] = trim(fgets($file));
and it seemed to work for me, though I haven’t checked the data.
I tried trim before but in that case it will print a single value as many time as csv count. I mean to say that if i’m comparing
if ($line_of_text[2] == $b[1]) {
echo $line_of_text[2] ; }
then it should only print 24 times because this value appears only 24 times in csv file. But it is printing 3507 times because csv has 3507 values.
OK, now I’m confused about what the code is actually supposed to do. When I run the above code, it shows me that, for $b[0], it finds that five times in the CSV file, for $b[1] it finds it once, for 2 it finds it five times, and so on.
Is it just printing “found” 3507 times because you’ve looped through the $b array, and every value in that column of the CSV file is in the array, so it finds them all? You don’t reset the $counte counter variable each time you change the array element that you’re trying to match, so at the end it has the total of all items found.
one more thing i want to ask is are you getting count value correctly?? I mean as you told for $b[0] it appears 5 time so while counting are you getting 5, bcz for me it is showing 1 for 5 times.
There isn’t a count variable in that code, other than $counte which never gets reset, so should just give the total number of each one found, which is probably equal to the number of lines in the CSV file.
Where is your count variable, where you do increment it, and where do you reset it?
The count variable i’m using in code is for total csv count. I want to count that if column value is equal to $b[0] then how many times it appears in csv . $b[0] appears 5 times, $b[1] appears 1 time, $b[2] appears 5 times like that. As of now it will just give 1111 for all it will not give total count for that particular value.
I’m really confused now - which line is producing the ‘1’ in that code, and how come the line that starts with ‘found’ isn’t producing that word? Is that the live code that produced that screen shot?
Yes it will, but then you display something entirely different. The count() function counts the number of elements in the array that you provide: http://php.net/manual/en/function.count.php