简体   繁体   中英

PHP array_search not working

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) "
"

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);
$current_array = array_map('trim', $current_trim);

也许FILE_IGNORE_NEW_LINES-标记也将为您工作。

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)...

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM