Jump to content

[SOLVED] Array_search/In_array problem


coldkill

Recommended Posts

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

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
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.