2

Hey guys and girls, i'm stumped. Trying to get array_search to work with this script.

 <?php 

$dir = '/var/www/html/pay.group.com/upload';
$i = 0;


if (is_dir($dir)) {
    if ($dh = opendir($dir)) {
        while (($file = readdir($dh)) !== false) {

            if ($file != "." && $file != ".."){
            //convert files from pdf to text
            exec("pdftotext /var/www/html/pay.group.com/upload/" . $file . " /var/www/html/tmp/converted/" . $file);
            //create array from text files
            $current_array = file("/var/www/html/tmp/converted/" . $file) or die ("<br/>**cannot find file to create array**"); 
            //search array
            echo array_search('EMPLOYEE NO. ',$current_array);      
            $i++;

            echo var_dump($current_array);
            }
        }
        closedir($dh);

        echo "$i files processed"; 
    }
}


?>

I get nothing from the array_search and I can't figure out why, its driving me mad.

Here is a relevant part of the var_dump that is working correctly.

"NON NEGOTIABLE " [28]=> string(5) "9871 " [29]=> string(13) "EMPLOYEE NO. " [30]=> string(1) " " [31]=> string(3) "01 " [32]=> string(6) "SHIFT " [33]=> string(1) " " [34]=> string(4) "MIC " [35]=> string(19) "LOCATION HRS/UNITS "

Is there something I am doing wrong? The string for the array search is exactly the same as it is in the actual array so I can't figure out why its not returning an array index for me.

Using the pre tag, this is what I get.

  [27]=>
  string(15) "NON NEGOTIABLE
"
  [28]=>
  string(5) "9871
"
  [29]=>
  string(13) "EMPLOYEE NO.
"
  [30]=>
  string(1) "
"
  [31]=>
  string(3) "01
"
  [32]=>
  string(6) "SHIFT
"
  [33]=>
  string(1) "
"
  [34]=>
  string(4) "MIC
"
  [35]=>
  string(19) "LOCATION HRS/UNITS
"
  [36]=>
  string(1) "
"
1
  • Probably it's a problem regarding the name of your files. I noticed someone else tried this piece of code and it worked. Commented Nov 29, 2010 at 20:02

4 Answers 4

3

The lines in the uploaded file are on separate lines. file() function leaves the newline characters attached to the array items and this the reason why search does not work.

You can strip newlines from all array items like this

array_walk($current_array, 'trim');

After that your search should work.

Or, as KingCrunch said, use

file("/var/www/html/tmp/converted/" . $file, FILE_IGNORE_NEW_LINES);
Sign up to request clarification or add additional context in comments.

2 Comments

See the end of my post I edited it to add what I am getting using the pre tag.
Omfg, its working, after hours and hours of trying to get this shit to work it finally does. I cannot thank you enough, seriously. I love you.
2
$current_array = array_map('trim', $current_trim);

Maybe the FILE_IGNORE_NEW_LINES-Flag will work for you also.

Comments

1

Not necessarily an answer, but perhaps you should try var_dump on your array search, sometimes integers (mostly 1 and 0, as in true and false) don't print out right. This will tell you exactly what the result is, rather that just not printing anything (which is what I assume happened, since you didn't say what it printed)...

2 Comments

I'm confused, I did use var_dump. What do you mean by use it on the array search?
var_dump(array_search('EMPLOYEE NO. ',$current_array)); instead of echo, so you can see the results with all the trimmings, though, this doesn't matter, problem seems solved :p
0

I tried to replicate your code and everything worked fine.

Maybe give in_array() a try. That will check if a value exists. If that doesn't work, try searching for the string without ending space.

2 Comments

I tried both ways you recommended, and its still not working at all, fml. I really have no idea why this isnt working. When you tried to run my code did you create the array in the php script?
Yes. I created an array myself. I see you have an answer already, though.

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.