Кто-нибудь сможет мне помочь, почему мой массив_поиск не работает в этом скрипте PHP, который я написал. Я уже много дней возился с этим и не могу понять эту проблему. Я пробовал перемещать array_search повсюду внутри цикла, вне цикла, и я получаю те же результаты. Я попытался найти различные значения массива, я попытался включить мои собственные функции для поиска массива, который я нашел в Интернете. Я знаю, что значения находятся в массиве, потому что у меня есть массивы, напечатанные в .txt-файле для отладки, и теперь я понятия не имею, что искать дальше. Есть идеи?PHP Array Поиск не работает
<?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. Рядом с каждым индексом I должен иметь индекс массива из каждой категории
6 pdf files converted
index found
index found
index found
index found
index found
index found
Что именно не работает? Можете ли вы выделить случай, что вы ищете, что находится в файле, и что не соответствует? Это будет работать лучше, чем огромный кусок кода. –
Я думаю, что вы переключили иглу и стог сена в свои аргументы ... '$ ArrSearch = array_search (« EMPLOYEE NO. », $ CurrArray);' –
@Brad Вы должны добавить это как ответ – meagar