Jump to content

How to use array_search for 1 sized array?


Tjorriemorrie

Recommended Posts

I have problems figuring this out. I expect it to remove the number and set the cookie again, but it gives this error:

 

Quest 132 not found in obtained:

Error in search!

array(1) {

  [ 0 ] =>

  string(3) "132"

}

 

Here's the code:

// remove qst, implode, and set
reset($obtained);
if ($key = array_search($questID, $obtained)) {
  unset($obtained[$key]);
  $adjObtained = implode(" ", $obtained);
  if (setcookie("obtained$s", $adjObtained, time()+86400*30)) {
    $goto = "setquests.php?sel=" . $s . "&drop=" . $questID;
    header("Location: $goto");
  } else {
    echo '<h1>Cookie could not be set!</h1>';
    ?><pre><?php var_dump($adjCompleted); ?></pre><?php
    exit;
  }
} else {
  echo "<h1>Quest $questID not found in obtained:</h1>";
  ?><pre><?php var_dump($obtained); ?></pre><?php
  exit;
}

 

Can anyone please help? It seems as soon as

the array is only 1 size big, it can't find the

value...or maybe it can't find the first value...

sigh, please help

Link to comment
Share on other sites

Thanks rhodesa!! that explains it!

 

OK, but it is not to say the result will be the first key, it could stretch into thousands in the end. So my code was all wrong, coz it has that error if it's the first record and returns 0 which equates to false.

 

Is this better?

if (array_search($questID, $obtained)) {

  $key = array_search($questID, $obtained);

  etc

 

Also, can I now take out the reset($array); ?

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.