simon551 Posted September 28, 2009 Share Posted September 28, 2009 $contractId="37"; if (in_array($contractId, $contractAdminArray)) { $admin='yes'; } else { $admin='no';} echo $admin; $contractAdminArrayString= implode(',',$contractAdminArray); echo $contractAdminArrayString; result no72,21,62,16,37,6,86,104,49,29,100,104 clearly 37 is in the array but the script says it's not. what am I doing wrong? Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/ Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Only thing that I can think of is maybe because they're off different data types, but even that shouldn't cause a problem. do this, right before the in_array() and post what it outputs: print_r($contractAdminArray); Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/#findComment-926514 Share on other sites More sharing options...
simon551 Posted September 28, 2009 Author Share Posted September 28, 2009 Array ( [0] => 72,21,62,16,37,6,86,104,49,29,100,104 ) no72,21,62,16,37,6,86,104,49,29,100,104 Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/#findComment-926519 Share on other sites More sharing options...
redarrow Posted September 28, 2009 Share Posted September 28, 2009 looks like a multidimensional array? would $contractAdminArray[0] solve this on the loop? is it repeating it self there use array_diff() function.... Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/#findComment-926520 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Oh, then it's clear why it isn't working.. in_array() checks all the elements of the array. You only have one element that contains a string.. You should explode the string in that element by ',' then check the array. Or use stripos() to check the string. Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/#findComment-926521 Share on other sites More sharing options...
simon551 Posted September 28, 2009 Author Share Posted September 28, 2009 oh. maybe there's a problem in how I'm creating the array. This is the code: $empid = $_COOKIE['empid'.$companyName]; $div = $_COOKIE['divisionId']; //contract manager mysql_select_db($database_conn_org, $conn_org); $qManager = "SELECT DISTINCT contract_teamdets.contractId as x FROM contract_teamdets WHERE ((contract_teamdets.teamRoleID = 1 OR contract_teamdets.teamRoleId=4) AND contract_teamdets.empID = $empid) "; $rsManager = mysql_query($qManager, $conn_org) or die(mysql_error()); $total_contracts_admin=mysql_num_rows($rsManager); ////start array my contracts////////////////////////////////////////////////////// $myContractsArray=array(); while ($row_rsManager = mysql_fetch_assoc($rsManager)) { array_push ($myContractsArray, $row_rsManager['x']); } //ownership items $qOwner = "SELECT DISTINCT contracts.contractId as x FROM contracts Inner Join tblclients ON contracts.clientId = tblclients.clientId WHERE tblclients.empID_relationshipHolder=$empid "; $rsOwner = mysql_query($qOwner, $conn_org) or die(mysql_error()); $total_contracts_admin=mysql_num_rows($rsOwner); ///continue array while ($row_rsOwner = mysql_fetch_assoc($rsOwner)) { array_push ($myContractsArray, $row_rsOwner['x']); } /////////////////////////////////////////////////////////////////////////////////////////////////// $myContractsArrayString= implode(',',$myContractsArray); $contractAdminArray=array(); //add any personal contracts to the new array before building up further. array_push ($contractAdminArray, $myContractsArrayString); if ($_SESSION['divisionAdmin']=='yes'){ $query_rsDivisionContracts = "SELECT contracts.contractId as x, contracts.divisionId FROM contracts WHERE contracts.divisionId = $div "; $rsDivisionContracts = mysql_query($query_rsDivisionContracts, $conn_org) or die(mysql_error()); //continue building array while ($row_rsDivisionContracts = mysql_fetch_assoc($rsDivisionContracts)) { array_push ($contractAdminArray, $row_rsDivisionContracts['x']); } } //continue building array if (($_SESSION['globalAdmin']=='yes')||($_SESSION['HRAdmin']=='yes')){ $query_rsGlobalContracts = "SELECT DISTINCT contracts.contractId as x FROM contracts"; $rsGlobalContracts = mysql_query($query_rsGlobalContracts, $conn_org) or die(mysql_error()); while ($row_rsGlobalContracts = mysql_fetch_assoc($rsGlobalContracts)) { array_push ($contractAdminArray, $row_rsGlobalContracts['x']); } } I have build one array that has a specific purpose and then I want to use that array in another array. I should find a better way to do that. Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/#findComment-926527 Share on other sites More sharing options...
Alex Posted September 28, 2009 Share Posted September 28, 2009 Can you please post your full source? It's hard to figure out the full problem when I'm partially blind-sighted. This doesn't look right though: $myContractsArrayString= implode(',',$myContractsArray); $contractAdminArray=array(); //add any personal contracts to the new array before building up further. array_push ($contractAdminArray, $myContractsArrayString); Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/#findComment-926531 Share on other sites More sharing options...
simon551 Posted September 28, 2009 Author Share Posted September 28, 2009 Yeah. I was creating a string and then dumping it in the array. that was creating an array with a string in it rather than adding each string element to the array which I wanted to do. I fixed it. Thanks for the help! <?php session_start(); require_once('../Connections/conn_org.php'); require_once('../security/adminTest.php'); $empid = $_COOKIE['empid'.$companyName]; $div = $_COOKIE['divisionId']; //contract manager mysql_select_db($database_conn_org, $conn_org); $qManager = "SELECT DISTINCT contract_teamdets.contractId as x FROM contract_teamdets WHERE ((contract_teamdets.teamRoleID = 1 OR contract_teamdets.teamRoleId=4) AND contract_teamdets.empID = $empid) "; $rsManager = mysql_query($qManager, $conn_org) or die(mysql_error()); $total_contracts_admin=mysql_num_rows($rsManager); ////start array my contracts////////////////////////////////////////////////////// $myContractsArray=array(); $contractAdminArray=array(); while ($row_rsManager = mysql_fetch_assoc($rsManager)) { array_push ($myContractsArray, $row_rsManager['x']); array_push ($contractAdminArray, $row_rsManager['x']); } //ownership items $qOwner = "SELECT DISTINCT contracts.contractId as x FROM contracts Inner Join tblclients ON contracts.clientId = tblclients.clientId WHERE tblclients.empID_relationshipHolder=$empid "; $rsOwner = mysql_query($qOwner, $conn_org) or die(mysql_error()); $total_contracts_admin=mysql_num_rows($rsOwner); ///continue array while ($row_rsOwner = mysql_fetch_assoc($rsOwner)) { array_push ($myContractsArray, $row_rsOwner['x']); array_push ($contractAdminArray, $row_rsOwner['x']); } /////////////////////////////////////////////////////////////////////////////////////////////////// $myContractsArrayString= implode(',',$myContractsArray); //add any personal contracts to the new array before building up further. //array_push ($contractAdminArray, $myContractsArrayString); if ($_SESSION['divisionAdmin']=='yes'){ $query_rsDivisionContracts = "SELECT contracts.contractId as x, contracts.divisionId FROM contracts WHERE contracts.divisionId = $div "; $rsDivisionContracts = mysql_query($query_rsDivisionContracts, $conn_org) or die(mysql_error()); //continue building array while ($row_rsDivisionContracts = mysql_fetch_assoc($rsDivisionContracts)) { array_push ($contractAdminArray, $row_rsDivisionContracts['x']); } } //continue building array if (($_SESSION['globalAdmin']=='yes')||($_SESSION['HRAdmin']=='yes')){ $query_rsGlobalContracts = "SELECT DISTINCT contracts.contractId as x FROM contracts"; $rsGlobalContracts = mysql_query($query_rsGlobalContracts, $conn_org) or die(mysql_error()); while ($row_rsGlobalContracts = mysql_fetch_assoc($rsGlobalContracts)) { array_push ($contractAdminArray, $row_rsGlobalContracts['x']); } } //array is complete ///turn it into a string to be used in a query $contractAdminArrayString= implode(',',$contractAdminArray); //$result=count($contractAdminArray); ?> Quote Link to comment https://forums.phpfreaks.com/topic/175828-solved-in_array-unexpected-problem/#findComment-926533 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.