Jump to content

Array Error


Dysan

Recommended Posts

Hi,

 

Why does the following error message get displayed, upon entering the first value/ID into the array? How do I prevent this error from happening?

 

Warning: in_array() [function.in-array]: Wrong datatype for second argument in C:\Documents and Settings\User\Desktop\Xampp\htdocs\1.php on line 7.

 

<?php
session_start();
$array = array();
$array = $_SESSION["somename"];


if (!in_array($_GET['id'], $array))
{
$array[] = $_GET['id'];
}
else
{
  echo "ID Exists";
}
$_SESSION["somename"] = $array;
print_r($array);
?>

Link to comment
Share on other sites

Does $_SESSION["somename"] actually contain an array? If it is PHP should not be outputting that error.

 

In the following bit of code:

$array = array();
$array = $_SESSION["somename"];

You setup a variable called $array as an array data type, however on the next line your assign it the value of the $_SESSION["somename"] variable. Now if that variable does not contain an array PHP will change $array's datatype to a string. WHich will result in the in_array function returning the above error message

Link to comment
Share on other sites

Hold on... I didn't read your script fully and now I understand what you are trying to do. You'll want to change the following two lines:

$array = array();
$array = $_SESSION["somename"];

to:

$array = ( isset($_SESSION['somename']) && is_array($_SESSION['somename']) ) ?  $_SESSION['somename'] : array();

 

EDIT: @snk

you cant send arrays directly check serialize() unsirialize() functions

www.php.net/serialize

PHP automaticaly serializes session data. No need to serialize/unserialize arrays being stored within a session.

 

Link to comment
Share on other sites

wildteen88

 

is it right to say..

 

in file a.php

<?php

//lets say session has been started

$Stringtest = "one-two-three";

$st2 = explode("-",$Stringtest);

 

$_SESSION['st'] = $st2;

?>

 

in file b.php

 

<?php

$st = $_SESSION['st'];

echo $st[0];

?>

 

will the result of echo be one ?

 

thanks and sorry if im sledding from the subject

 

Link to comment
Share on other sites

The following line is an in-line if/else statement (most commonly called a ternary operator):

$array = ( isset($_SESSION['somename']) && is_array($_SESSION['somename']) ) ?  $_SESSION['somename'] : array();

Converted into a normal if/else statement:

if( isset($_SESSION['somename']) && is_array($_SESSION['somename']) ) 
{
    $array = $_SESSION['somename'];
}
else
{
    $array = array();
}

Link to comment
Share on other sites

wildteen88

 

is it right to say..

 

in file a.php

<?php

//lets say session has been started

$Stringtest = "one-two-three";

$st2 = explode("-",$Stringtest);

 

$_SESSION['st'] = $st2;

?>

 

in file b.php

 

<?php

$st = $_SESSION['st'];

echo $st[0];

?>

 

will the result of echo be one ?

 

thanks and sorry if im sledding from the subject

 

Correct

Link to comment
Share on other sites

How do I create a displayShoppingCart() function, that displays the total amount of items inside the array, or displays the message "Shopping Cart is Empty", if the array is empty?

 

How do I create a switch statement, based on a action variable, then add the code to add data to the array to a case statement called "add" inside the switch statement?

 

Each time an item/id is added to the array, how do I display the shopping cart.

Link to comment
Share on other sites

mine is easier :P

 

thanks for the post i need to use your code, i have a similar issue.

You are right, yours is simpler. But yours is very very very specific and will work only if the url ends with "id=<num>"

Mine will work even if there's more url parameters, not only the id, and id doesn't have to be in the end to make it work- it can be in the middle: "domain.com/data.php?bla=1&id=2&bla2=44" Will match.

 

Orio.

Link to comment
Share on other sites

Why doesn't the amount of values in the array get displayed, to reflect what in the array, upon calling the writeShoppingCart() function? - The amount stays at 0, Why is this?

 

<?php
session_start();

function writeShoppingCart()
{
echo count($array);
}

if(isset($_SESSION['ids']) && is_array($_SESSION['ids']))
{
    $array = $_SESSION['ids'];
}
else
{
    $array = array();
}


if (!in_array($_GET['id'], $array))
{
$array[] = $_GET['id'];
}
else
{
  echo "ID Exists";
}
$_SESSION['ids'] = $array;
writeShoppingCart()
print_r($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.