Dysan Posted November 21, 2007 Share Posted November 21, 2007 Why doesn't the following code work?, I keep getting greeted with the following error message: Warning: array_key_exists() [function.array-key-exists]: The second argument should be either an array or an object in C:\Documents and Settings\User\Desktop\Xampp\htdocs\1.php on line 7. <?php session_start(); if (array_key_exists($_GET['id'], $array)) { echo "Already Exists!"; } else { $_SESSION['ids'][] = $_GET['id']; $array = $_SESSION['ids']; } print_r($array); ?> Quote Link to comment Share on other sites More sharing options...
revraz Posted November 21, 2007 Share Posted November 21, 2007 What is in $array? Quote Link to comment Share on other sites More sharing options...
PHP_PhREEEk Posted November 21, 2007 Share Posted November 21, 2007 Of course it doesn't work. RTM... dump your variables http://us3.php.net/manual/en/function.array-key-exists.php PhREEEk Quote Link to comment Share on other sites More sharing options...
Dysan Posted November 21, 2007 Author Share Posted November 21, 2007 What is in $array? ID's. I want the ID retrieve from another page, to only be added to the array if it doesn't already exist in the array. If it does, a message should be displayed notifying the user that it already exists. How do I do this? Quote Link to comment Share on other sites More sharing options...
premiso Posted November 21, 2007 Share Posted November 21, 2007 What he is trying to say, no where in that code is $array defined before the if statement it is used in. Which is why there is an error. If there is more code you are omitting, please post it. Otherwise where are you getting the values of $array from? Quote Link to comment Share on other sites More sharing options...
trq Posted November 21, 2007 Share Posted November 21, 2007 <?php session_start(); if (is_array($array) && array_key_exists($_GET['id'], $array)) { echo "Already Exists!"; } else { $_SESSION['ids'][] = $_GET['id']; $array = $_SESSION['ids']; } print_r($array); ?> Quote Link to comment 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.