Jump to content

in_array


We Must Design

Recommended Posts

I have created an array by using the following code:

 

$_SESSION['favoriteArray'][] = $_GET['cocktail'];

 

This works fine. Then I want to use the in_array function to see if a variable is in an the array before I add it. This is where the problem is, I am not sure I have the right syntax. Here is the code anyway:

 

if (in_array ($cocktail_name, $_SESSION['favoriteArray'][]);

 

and here is the error message:

 

Fatal error: Cannot use [] for reading in /home/must/public_html/cocktails/index.php on line 74

 

Thanks, Chris

Link to comment
Share on other sites

<?php
$some_variable = 1;
$some_array = array(1, 'lump', 'or', 2);

if (in_array($some_variable, $some_array)) {
    echo('Muffins.');
}
?>

 

That's how you use it, but you're problem is putting [] after your session variable, you can't do that...

 

$_SESSION['favoriteArray'][]

 

...as far as I know.

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.