coldkill Posted August 26, 2007 Share Posted August 26, 2007 Hello. I'm currently have a problem searching an array created by the explode function. The original string comes from a file and is then exploded with a newline separator. Anyway the problem seems to be that nothing is returned from the function. Not even False or 0. Here's the code. define( "ERROR_SET", "DBC2" ); /* * Get the error file */ $error_file = 'source/data/display/error.cfdat'; $f = fopen( $error_file, "r" ); $contents = fread( $f, filesize( $error_file ) ); /* * Seperate the list */ $list = explode( "\n", $contents ); /* * Search the list for the error code */ $error = in_array( ERROR_SET, $list ); echo $error; Any ideas? Thanks for the help. Cold Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/ Share on other sites More sharing options...
Wuhtzu Posted August 26, 2007 Share Posted August 26, 2007 Well, you print the return value of the in_array() function which is TRUE on success and FALSE on error. I did a simple test and these are the results: This displays "1" in my browser <?php $array = array('a','b','c','d'); $in_array_return_value = in_array('a',$array); echo $in_array_return_value; ?> This displays "" (nothing) in my browser <?php $array = array('a','b','c','d'); $in_array_return_value = in_array('asdf',$array); echo $in_array_return_value; ?> So it seems like printing the return value will display 1 for true and nothing false.... So in your case your case the error you are looking for does not exist in the array, therefore false is returned and nothing is printed to the screen or ERROR_SET isn't defined properly... Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334681 Share on other sites More sharing options...
Barand Posted August 26, 2007 Share Posted August 26, 2007 try something like echo (in_array( ERROR_SET, $list )) ? 'FOUND' : 'NOT FOUND'; Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334682 Share on other sites More sharing options...
Wuhtzu Posted August 26, 2007 Share Posted August 26, 2007 Which is the same as: <?php if(in_array(ERROR_SET, $list)) { echo "Found"; } else { echo "Not found"; } ?> in case you didn't know. Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334691 Share on other sites More sharing options...
coldkill Posted August 26, 2007 Author Share Posted August 26, 2007 Ok the main point was the Array_search function which returns nothing for this yet works fine for a normal array. Any ideas? Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334722 Share on other sites More sharing options...
Wuhtzu Posted August 26, 2007 Share Posted August 26, 2007 It is kind of weird to have something which is not being mentioned once to be the main point. array_search() does not appear in your code and you do not speak about it in your topic. Where in your code do you use array_search() and what is a normal array compared to the array (which array?) you are talking about? Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334728 Share on other sites More sharing options...
coldkill Posted August 26, 2007 Author Share Posted August 26, 2007 I changed in_array() for array_search(). My original code used the array_search function to find the array key for the object found. Exactly the same usage though using the $list array. Cold Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334748 Share on other sites More sharing options...
coldkill Posted August 26, 2007 Author Share Posted August 26, 2007 Also just a note. I tried printing the result from the in_array function and it displayed nothing. Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334771 Share on other sites More sharing options...
teng84 Posted August 26, 2007 Share Posted August 26, 2007 i guess the prob is the coder not php try http://www.php.net/manual/en/function.in-array.php that explains this issue the example given are good so i dont know why its not working for you Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-334776 Share on other sites More sharing options...
coldkill Posted August 27, 2007 Author Share Posted August 27, 2007 Actually I've read that quite a few times to see if it could help me further. It hasn't. The code still does not work and still returns nothing. Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-335003 Share on other sites More sharing options...
teng84 Posted August 27, 2007 Share Posted August 27, 2007 did you already use print_r for that to see if your array is an array and has a value Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-335007 Share on other sites More sharing options...
coldkill Posted August 27, 2007 Author Share Posted August 27, 2007 Yep. The array does exist and contains the values it should from the file. Here is what was returned: Array ( [0] => DBC1; Unable to connect to database; An attempt to establish a connection to the database has failed [1] => DBC2; Unable to select database; An attempt to select the database has failed [2] => DBC3; Connection not established; A connection has not been established to the database yet [3] => DBC4; Connection timed out; The connection to the database has been closed prematurely [4] => DBQ1; Query not valid; The query requested cannot be executed [5] => S1; Style not set; There is an error with the style settings [6] => SP1; Page does not exist; Error 404, the page requested does not exist [7] => SP2; Malformed Page Entry; The page entry is malformed and cannot be displayed ) Ok I've figured out why it's not working now. It's searching the entire value from the array instead of searching within it. Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-335304 Share on other sites More sharing options...
Barand Posted August 27, 2007 Share Posted August 27, 2007 Alter the array to be $list = array ( 'DBC1' = 'Unable to connect to database; An attempt to establish a connection to the database has failed' , 'DBC2' = 'Unable to select database; An attempt to select the database has failed' , 'DBC3' = 'Connection not established; A connection has not been established to the database yet' , etc ); Then you just need to do $srch = 'DBC2'; if (isset($list[$srch])) // EXISTS else // NOT EXISTS Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-335396 Share on other sites More sharing options...
coldkill Posted August 27, 2007 Author Share Posted August 27, 2007 Only problem is the data is coming from a file not an array function. I guess I'll have to work something out for it. Thanks for your help all. Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-335443 Share on other sites More sharing options...
coldkill Posted August 27, 2007 Author Share Posted August 27, 2007 Ok final product for anyone who would like to know. Since I'm not using PHP 5 I've had to use a for control structure to create an array with the keys and values from the file. <?PHP define( "ERROR_SET", "DBC2" ); /* * Get the error file */ $error_file = 'source/data/display/error.cfdat'; $f = fopen( $error_file, "r" ); $contents = fread( $f, filesize( $error_file ) ); /* * Seperate the list */ $array = explode( ":;\n", $contents ); $list = explode( "\n", $array[1] ); $codes = explode( ":", $array[0] ); for( $i = 0; $i < count( $codes ); $i++ ) { $error[$codes[$i]] = $list[$i]; } /* * Search the list for the error code */ if( array_key_exists( ERROR_SET, $error ) ) { //found } else { //not found } ?> Thanks for all the help. Cold Link to comment https://forums.phpfreaks.com/topic/66791-solved-array_searchin_array-problem/#findComment-335469 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.