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
https://forums.phpfreaks.com/topic/78440-array-error/
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-396959
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-396968
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-396981
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-396982
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-396983
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-397029
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-397035
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
https://forums.phpfreaks.com/topic/78440-array-error/#findComment-397050
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.