-3

I need to check if in my array that come from database (SQLSERVER, so odbc) I have a some values.

I have this code:

$dbhandle = odbc_connect("Driver={SQL Server Native Client 11.0};Server=$myServer;Database=$myDB;", $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
$query = "SELECT NumNation, NumPlayers FROM table1";
$result = odbc_exec($dbhandle, $query);

while($rows = odbc_fetch_array($result)){
$myArray[] = $rows['NumNation']."=>".$rows['NumPlayers']; //this

Or I can have also this:

$myArray[] = $rows; //or this
} // close while
echo ("<br>".implode(",<br>",$myArray).".<br>");

Then I have some other code for select from another database tables and then I want to check if in my array (from the first table) I have this data or not.

I wrote this for example:

$NumNation = '123'
$Players   = '456'

if (in_array($NumNation."=>".$NumPlayers, $myArray)) continue;

and the continue with my code for save and something else.

But the check doesn't work.

Why?

If I var_dump myArray I have this:

array(363) { [0]=> string(31) " =>2000-12-31 00:00:00" [1]=> string(31) " =>2000-12-31 00:00:00" [2]=> string(31) " =>2000-12-31 00:00:00" [3]=> string(36) " 15=>2001-02-28 00:00:00" [4]=> string(36) " 18=>2001-02-28 00:00:00" [5]=> string(36) " 18=>2001-02-28 00:00:00" [6]=> string(36) " 14=>2001-02-15 00:00:00" [7]=> string(36) " 12=>2001-02-10 00:00:00" ....so on until......[347]=> string(31) " 4 =>2013-07-23 00:00:00" [348]=> string(31) " 5 =>2013-07-23 00:00:00" [349]=> string(2) "=>" [350]=> string(36) " 011013=>2013-10-01 00:00:00" [351]=> string(36) " 011013=>2013-10-01 00:00:00" [352]=> string(36) " 011213=>2013-12-01 00:00:00" [353]=> string(36) " 011213=>2013-12-01 00:00:00" [354]=> string(36) " 0112131=>2013-12-01 00:00:00" [355]=> string(36) " 0112131=>2013-12-01 00:00:00" [356]=> string(36) " 100=>2014-11-30 00:00:00" [357]=> string(31) " 1 =>2014-12-02 00:00:00" [358]=> string(31) " 1 =>2014-12-02 00:00:00" [359]=> string(31) " 1 =>2014-12-02 00:00:00" [360]=> string(26) "00032=>2014-02-12 00:00:00" [361]=> string(26) "00032=>2014-02-12 00:00:00" [362]=> string(26) "00017=>2014-02-12 00:00:00" }
5
  • What doesn't work? Do you get an error? Have you var_dumped the $myArray? Your second example doesn't really look relavent to what your in_array is looking for. Commented Dec 20, 2014 at 11:08
  • The while loop in the first snippet has no closing brace. Commented Dec 20, 2014 at 11:31
  • Also if I have in the array the couple (Numnation => 123 and Players => 456) the check say me the couple isn't. Commented Dec 20, 2014 at 11:31
  • Yes I know Palec, is an example, for understanding logic. Commented Dec 20, 2014 at 11:32
  • @AntonyD'Andrea, I var_dumped myArray. I edited the question. Commented Dec 20, 2014 at 11:37

1 Answer 1

0

Try this :

$dbhandle = odbc_connect("Driver={SQL Server Native Client 11.0};Server=$myServer;Database=$myDB;", $myUser, $myPass) or die("Couldn't connect to SQL Server on $myServer");
$query = "SELECT NumNation, NumPlayers FROM table1";
$result = odbc_exec($dbhandle, $query);
$myArray = array();
while($rows = odbc_fetch_assoc($result)){
     $myArray[$rows['NumNation']] = $rows['NumPlayers'];
}

$NumNation = 123;
$NumPlayers = 456;
ksort($myArray);
foreach($myArray as $key=>$val) {
     if($key == $NumNation && $val == $NumPlayers) {
         continue;
     }
}
Sign up to request clarification or add additional context in comments.

2 Comments

I think the foreach is a bit heavy. Do not you think?
Do you know any better ways ?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.