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
https://forums.phpfreaks.com/topic/72426-in_array/
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
https://forums.phpfreaks.com/topic/72426-in_array/#findComment-365226
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.