PHP数组搜索不起作用

时间:2010-11-19 16:20:43

标签: php arrays search

是否有人能够帮助我解决为什么我的array_search在我编写的PHP脚本中无效。我已经搞砸了好几天了,无法弄清楚这个问题。我试过在整个地方,循环内部,循环外部移动array_search,我得到了相同的结果。我试过搜索不同的数组值,我已经尝试包含我自己的函数来搜索我在网上找到的数组。我知道值在数组中,因为我将数组打印到.txt文件进行调试,现在我不知道接下来要查找什么。有任何想法吗?

   <?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++;   

        }           
    }
}
?>

编辑:我根据您的结论修改了代码,我也在这里更新了代码。使用上面的代码,这是我在尝试转换6个pdf文件时得到的输出。在每个索引旁边,我应该有每个搜索的数组索引

6 pdf files converted 

index found 

index found 

index found 

index found 

index found 

index found 

4 个答案:

答案 0 :(得分:4)

我认为你已经在论证中切换了针和大海捞针......

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

根据要求,评论已回复

@meagar:不确定这是不是问题,或者他们是否已经筋疲力尽,只是在两天的挫折之后尝试了任何事情。

答案 1 :(得分:1)

array_search( needle haystack

你不是有那种错吗? (在 haystack 中搜索 needle ,而不是相反)。

答案 2 :(得分:0)

$ idx = array_search($ needle,$ haystack);

你需要在代码中交换needle和haystack

答案 3 :(得分:0)

我猜你正在寻找拥有员工编号的行,例如“EMPLOYEE NO.7”等等。如果您使用array_search("EMPLOYEE NO. ", $currArray),则会搜索与完全“EMPLOYEE NO。”匹配的行,这样您就不会获得任何匹配。

如果您要搜索“EMPLOYEE NO。”开头的值,您必须遍历数组并使用strpos()查找匹配项。