9 Replies - 1997 Views - Last Post: 25 October 2010 - 04:21 PM Rate Topic: -----

#1 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

array_search not working

Posted 22 October 2010 - 10:35 AM

<?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.miste.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.mis.com/upload/" . $file . " /var/www/html/pay.mis.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 "I counted $nodeCount pdf files";
echo "<br />";

//open the directory
if ($handle2 = opendir('/var/www/html/pay.mis.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.mist.com/upload/arrays/" . $currentText . ".txt", "w+") or die ("Cannot find file to create array, ID 2");
	   		
	   
	   		//read the file we are on from the loop into the array 
	   		$currentArray = file("/var/www/html/pay.mist.com/upload/converted/" . $currentText, FILE_SKIP_EMPTY_LINES) or die ("Cannot find file to create array, ID 1");
	   		
	   		//search array and print to screen
			$x = array_search("EMPLOYEE NO.", $currentArray);
       		echo "Employee # index: $x";
	   		echo "<br />"; 
			
	   		
	   		//print array to .txt file for debugging purposes
	   		$out = print_r($currentArray, true);
	   		fwrite($createArray, $out);
	   		fclose($createArray);
	   	  		
	   					
    	}           
	}
}

?>


I'm not sure why, google is no help. array_search is not returning a value even though I know its in the array. Any ideas?

Edit:here is a link to my last thread for some more info on the subject.

This post has been edited by JordanH: 22 October 2010 - 10:42 AM


Is This A Good Question/Topic? 0
  • +

Replies To: array_search not working

#2 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 22 October 2010 - 12:00 PM

They are both strings. tested using gettype. Yet, the in array function tells me the string does not exist. When I check my printed .txt file that contains the array, its there.
Was This Post Helpful? 1
  • +
  • -

#3 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 22 October 2010 - 12:36 PM

It was a file permissions error the whole time.... :wheelchair:

Edit: I lied, its still not working and I have no idea why.

I can print the array using print_r($currentArray) successfully. Array search still gives me nothing.

This post has been edited by JordanH: 22 October 2010 - 12:51 PM

Was This Post Helpful? 0
  • +
  • -

#4 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,014
  • Joined: 08-August 08

Re: array_search not working

Posted 22 October 2010 - 06:14 PM

It's likely getting an extra space in the string. Try using trim on it:
http://php.net/manua...nction.trim.php

This post has been edited by CTphpnwb: 22 October 2010 - 06:20 PM

Was This Post Helpful? 2
  • +
  • -

#5 ShaneK  Icon User is offline

  • require_once("brain.php"); //Fatal error :/
  • member icon

Reputation: 240
  • View blog
  • Posts: 1,224
  • Joined: 10-May 09

Re: array_search not working

Posted 23 October 2010 - 04:38 AM

View PostJordanH, on 22 October 2010 - 12:36 PM, said:

It was a file permissions error the whole time.... :wheelchair:

Edit: I lied, its still not working and I have no idea why.

I can print the array using print_r($currentArray) successfully. Array search still gives me nothing.


Sometimes it's best to use var_dump(), as it will tell you exactly how many characters there are in each string. It also puts double quotation marks around the string, so you can see if there are any extra spaces.

Yours,
Shane~

This post has been edited by ShaneK: 23 October 2010 - 04:39 AM

Was This Post Helpful? 1
  • +
  • -

#6 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 25 October 2010 - 11:46 AM

View PostShaneK, on 23 October 2010 - 03:38 AM, said:

View PostJordanH, on 22 October 2010 - 12:36 PM, said:

It was a file permissions error the whole time.... :wheelchair:

Edit: I lied, its still not working and I have no idea why.

I can print the array using print_r($currentArray) successfully. Array search still gives me nothing.


Sometimes it's best to use var_dump(), as it will tell you exactly how many characters there are in each string. It also puts double quotation marks around the string, so you can see if there are any extra spaces.

Yours,
Shane~


Thanks, there is a extra space after EMPLOYEE NO. So, I accounted for that and its still not working right. when i var_dump(), this is what I get. I cut out all info and only left what I thought was applicable, I also cut off the end of the array since there are two pay stubs in it and I didn't feel like editing it all. You can see "EMPLOYEE NO. " has an extra space at the end but the code still is not working right.

array(1) { [0]=> array(167) { [0]=> string(88) ". . . . . . . . . .*********************** Bank " [1]=> string(12) "DATE AMOUNT " [2]=> string(1) " " [3]=> string(11) "***********8 " [4]=> string(1) " " [5]=> string(8) "$453.** " [6]=> string(1) " " [7]=> string(4) "PAY " [8]=> string(1) " " [9]=> string(46) "*******************************8 " [10]=> string(1) " " [11]=> string(16) "TO THE ORDER OF " [12]=> string(1) " " [13]=> string(59) "************************************* " [14]=> string(1) " " [15]=> string(17) "Bank Routing No. " [16]=> string(1) " " [17]=> string(20) "Bank Account Number " [18]=> string(1) " " [19]=> string(15) "Deposit Amount " [20]=> string(1) " " [21]=> string(10) "*********** " [22]=> string(1) " " [23]=> string(17) "************ " [24]=> string(1) " " [25]=> string(7) "**** " [26]=> string(1) " " [27]=> string(15) "NON NEGOTIABLE " [28]=> string(5) "**** " [29]=> string(13) "EMPLOYEE NO. " [30]=> string(1) " " [31]=> string(3) "0** " [32]=> string(6) "SHIFT " [33]=> string(1) " " [34]=> string(4) "*** " [35]=> string(19) "LOCATION HRS/UNITS " [36]=> string(1) " " [37]=> string(14) "*********** " [38]=> string(42) "EMPLOYEE NAME CURRENT AMOUNT YEAR TO DATE " [39]=> string(1) " " [40]=> string(11) "******* " [41]=> string(11) "PERIOD END " [42]=> string(1) " " [43]=> string(9) "EARNINGS " [44]=> string(1) " " [45]=> string(11) "DEDUCTIONS " [46]=> string(1) " " [47]=> string(15) "CURRENT AMOUNT " [48]=> string(1) " " [49]=> string(13) "YEAR TO DATE " [50]=> string(1) " " [51]=> string(50) "Regular Pay Overtime Pay Vacation Pay Holiday Pay " [52]=> string(1) " " [53]=> string(25) "3*** ******** " [54]=> string(1) " " [55]=> string(25) "*********** " [56]=> string(1) " " [57]=> string(30) "**************" [58]=> string(1) " " [59]=> string(99) "Federal ******8Tax FICA Tax**********Income *********** Ins. Co-Pay Dental Co-Pay " [60]=> string(1) " " [61]=> string(39) "******************* " [62]=> string(1) " " [63]=> string(50) "***************************" [64]=> string(1) " " [65]=> string(7) "*****0 " [66]=> string(17) "CURRENT EARNINGS " [67]=> string(1) " " [68]=> string(7) "****0 " [69]=> string(19) "************* " [70]=> string(1) " " [71]=> string(7) "**** " [72]=> string(8) "NET PAY " [73]=> string(1) " " [74]=> string(9) "**** " [75]=> string(16) "Y.T.D. EARNINGS " [76]=> string(1) " " [77]=> string(8) "***** " [78]=> string(18) "Y.T.D. DEDUCTIONS " [79]=> string(1) " " [80]=> string(9) "**** " [81]=> string(15) "Y.T.D. NET PAY " [82]=> string(1) " " [83]=> string(89) ". . . . . . . . . . ********************************** " [84]=> string(12) "DATE AMOUNT " [85]=> string(1) " " [86]=> string(11) "******* " [87]=> string(1) " " [88]=> string(8) "$*** " [89]=> string(1) " " [90]=> string(4) "PAY " [91]=> string(1) " " [92]=> string(46) "************************* " [93]=>  string(1) "" } }

Was This Post Helpful? 0
  • +
  • -

#7 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,014
  • Joined: 08-August 08

Re: array_search not working

Posted 25 October 2010 - 12:39 PM

How did you account for that? Did you use trim()?

Specifically, I'm thinking of example #2.

This post has been edited by CTphpnwb: 25 October 2010 - 12:45 PM

Was This Post Helpful? 1
  • +
  • -

#8 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 25 October 2010 - 01:22 PM

View PostCTphpnwb, on 25 October 2010 - 11:39 AM, said:

How did you account for that? Did you use trim()?

Specifically, I'm thinking of example #2.


I did not use trim, I just did a var_dump($array), looked at the array and seen that there was an extra space at the end of array index 29. How can I trim an array value if I have to search for it first? Thanks for the help!
Was This Post Helpful? 0
  • +
  • -

#9 JordanH  Icon User is offline

  • New D.I.C Head

Reputation: -1
  • View blog
  • Posts: 46
  • Joined: 28-February 09

Re: array_search not working

Posted 25 October 2010 - 01:53 PM

Even if i do use trim, how will that make array_search work?
Was This Post Helpful? 0
  • +
  • -

#10 CTphpnwb  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 2889
  • View blog
  • Posts: 10,014
  • Joined: 08-August 08

Re: array_search not working

Posted 25 October 2010 - 04:21 PM

If you trim the extra spaces then you can look for the item without worrying about extra spaces! These possible values:
"EMPLOYEE NO. " 
"EMPLOYEE NO.  "
" EMPLOYEE NO. "

end up as this:
"EMPLOYEE NO."

so when you search for it, you'll find it!
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1