dongyue6199 2010-11-19 16:20
浏览 55

PHP数组搜索不起作用

Would anyone be able to help me out with why my array_search is not working in this PHP script I have written. I've been messing with this for days and cannot figure out this problem. I've tried moving array_search all over the place, inside the loop, outside the loop, and I'm getting the same results. I have tried searching for different array values, I've tried including my own functions for searching the array that i found online. I know the values are in the array because I have the arrays printed to a .txt file for debugging, and now I have no idea what to look for next. Any ideas?

   <?php 
//this variable tells us how many drupal nodes or 'paystub pages' we need to create
$nodeCount = 0;
$i = 0;
//needed for creating a drupal node
//for this code to work this script must be run from the root of the drupal installation
require_once './includes/bootstrap.inc';
drupal_bootstrap(DRUPAL_BOOTSTRAP_FULL);
if ($handle = opendir('/var/www/html/pay.*********group.com/upload')) 
{
    /* This is the correct way to loop over the directory. */
    while (false !== ($file = readdir($handle))) 
    {
       if ($file != "." && $file != "..") 
       {
            $nodeCount++;
            //We convert the pdf documents into text documents and move put them in the converted folder
            $command = "pdftotext /var/www/html/pay.*********group.com/upload/" . $file . " /var/www/html/pay.*********group.com/upload/converted/" . $file . ".txt";
            //Execute the command above
            $output = exec($command);

        }   
    }       
    closedir($handle);      
}
//subtract two because the folders "array" and "converted" are included because PHP does not differentiate
//between folders and files
$nodeCount = $nodeCount - 2; 
echo "<br />";
echo "<b> $nodeCount pdf files converted </b>";
echo "<br />";
//open the directory
if ($handle2 = opendir('/var/www/html/pay.*********group.com/upload/converted')) 
{

    //check to see if we have reached the last file of our directory, if not stay in loop
    while (false !== ($currentText = readdir($handle2))) 
    {
        //filter out files named . and ..
       if ($currentText != "." && $currentText != "..") 
       {
            //Create a file for array to be printed to
            $createArray = fopen("/var/www/html/pay.*********group.com/upload/arrays/" . $currentText, "w+") or die ("Cannot find file to create array, ID 2");


            //read the file we are on from the loop into the array 
            $currArray = file("/var/www/html/pay.*********group.com/upload/converted/" . $currentText) or die ("Cannot find file to create array, ID 1");


            $ArrSearch = array_search("EMPLOYEE NO. ", $currArray);

            echo "<br />";
            echo "<b> $ArrSearch index found </b>";
            echo "<br />";

            //array_trim($currArray[$i]);           
            //var_dump($currArray[$i]);

            //print array to .txt file for debugging purposes
            $out = print_r($currArray, true);
            fwrite($createArray, $out);
            fclose($createArray);
            $i++;   

        }           
    }
}
?>

edit: I fixed the code based on your conclusions, I updated the code here too. With the code above this is the out put I get while trying to convert 6 pdf files. Next to each index I should have an array index from each search

6 pdf files converted 

index found 

index found 

index found 

index found 

index found 

index found 
  • 写回答

4条回答 默认 最新

  • doufendi9063 2010-11-19 16:30
    关注

    I think you've switched the needle and the haystack in your arguments...

    $ArrSearch = array_search("EMPLOYEE NO. ", $currArray);
    

    As requested, comment gone to answer

    @meagar: wasn't sure if that was the problem, or if they exhausted that and are just trying anything after two days of frustration.

    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分