I am creating a method to pull information from one table, check if it exists in a second table, and if it doesn't then insert the information. While comparing the data, the check must first see if the name matches, and then proceed to check if the account number matches the corresponding name. This code is heavily commented so a step by step process can be followed as well. I am greatly appreciative of all the help the masters of stackexchange bring to the table, so after scratching my head for a while, I bring my issues to the table.
Apparently my code is breaking when it goes to check the account number, even though a matching name exists. According to my best skills, all the code should work perfectly.
As you will be able to see through the print_f of all_validation_names, as well as all_validation_accounts, the information of the first passing statement is contained in the array with a check.
My question is as follows: Why can't a varchar variable, stored in an array with a corresponding key ($check_account_number) be passed into in_array. The function always returns else, where although the variable is "1", it isn't found using the corresponding key in $all_validation_accounts.
Please help!
if (in_array($check_agency_name,$all_validation_names))
{
//Set a marker for the key index on which row the name was found
$array_key= array_search($check_agency_name, $all_validation_names);
//Now check if the account corresponds to the right account name
if (in_array($check_account_number,$all_validation_accounts["$array_key"]))
//NOTE: i've even tried strict comparison using third parameter true
{
//Bounce Them If the Account Matches with a name So theres no duplicate entry (works)
}
//If the account doesn't correspond to the right account name, enter the information
else
//ALSO DOESNT WORK: if (!(in_array($check_account_number,$all_validation_accounts["$array_key"])))
{
// The account doesn't exist for the name, so enter the information
//CODE TO ENTER INFORMATION THEN BOUNCE (works)
break;
}
}
Passing Name to match: a And Account:1
There are accounts existing, so heres all the validation arrays: all_validation_names: Array ( [0] => a [1] => a [2] => b )
all validation accounts: Array ( [0] => 1 [1] => 2 [2] => 2 )
the system found check_agency_name in the array all_validation_names, so lets check the account number with this key: 0
the account name existed, but the system didn't find the account number match, so it must enter it as if the account never existed
heres the information that should be entered: used_selector: 0 agency_name:A address: city: state: zip: account_number: 1
now the system should enter the info, and bounce the client out
I have answered my own question by rethinking the logic behind the checks.
Thank you for all your responses.
Here is a link to the solution. I have posted it online to share as well as optimize, because all the great minds of SO deserve it!
Warning: this is not the answer to the question
Lets start from your trim on steroids:
Better optimize it with in built php functions as follows:
function __trim($str, $charlist = '') {
$result = '';
/* list of forbidden chars to be trimmed */
$forbidden_list = array(" ", "\t", "", "
", "\0", "\x0B");
if (empty($charlist)) {
for ($i = 0; $i < strlen($str); $i++){
if(!in_array($str[$i],$forbidden_list))$result .= $str[$i];
}
} else { // else means in case if (condition) was not met
for ($i = 0; $i < strlen($str); $i++) {
if(strpos($charlist, $str[$i])===false)$result .= $str[$i];
}
}
return ($result);
}
Ritual Lecture
Stop using mysql. It is deprecated. Using it means inviting trouble.
This is not an answer. Your code is full of redundancies and is extremely difficult to read. You need to do a lot more work on your own to narrow down the issue before you come to SO for assistance. It's unlikely anyone will have the patience to step through your code, especially since they also can't run it without having a copy of your invisible database.
However, I felt obligated to tell you that your __trim()
function is poorly named and far too complicated for what it does. It is not a "trimmer", since unlike the real trim()
it removes characters from the "inside" of strings as well as the ends. And your implementation is far too complex.
It is the same as this one-line function:
function __trim($str, $charlist=" \t
\0\x0B") {
return str_replace(str_split($charlist), '', $str);
}
Once I saw that forty-line __trim
function I lost the will to read any more code in the question.
You've narrowed down your question, good. I'll attempt to infer the state of the program at this point. (This is something you should have done for us....)
Slimming your code to very minimum:
if (in_array($check_agency_name, $all_validation_names)) {
$array_key = array_search($check_agency_name, $all_validation_names);
if (in_array($check_account_number, $all_validation_accounts["$array_key"])) {
error_log('IF-BRANCH');
} else {
error_log('ELSE-BRANCH');
// break; // Removed because it makes no sense here.
}
}
Let's see what the values of these vars are:
$check_agency_name = 'a'
$all_validation_accounts = array(1, 2, 2);
$all_validation_names = array('a', 'a', 'b');
$array_key = "0";
$check_account_number = ???
You haven't told us what $check_account_number
is. However we still know enough to see at least one thing that is wrong. The following lines are all the same (just replacing variable names with values):
in_array($check_account_number, $all_validation_accounts["$array_key"])
in_array($check_account_number, $all_validation_accounts["0"])
in_array($check_account_number, $all_validation_accounts[0])
in_array($check_account_number, 1)
Here we see that regardless of the value of $check_account_number
, in_array()
will return false because its second argument is not an array!
(By the way, don't quote key lookups: $all_validation_accounts[$array_key]
is more straightforward--the quoting accomplishes nothing except to obscure your intent.)
This code would also produce an error Warning: in_array() expects parameter 2 to be array, integer given in /file/name.php on line ##
. This points you to the problem right away, and you would have seen it if you had checked your error logs. Set your PHP error reporting very high (E_ALL | E_STRICT
), watch your error logs, and make sure your program does not produce any error output while running normally. If you see anything, fix your code.