Forum Moderators: coopster

Message Too Old, No Replies

phantom value being inserted into array

         

triangulum

12:14 am on Jun 17, 2006 (gmt 0)

10+ Year Member



Can anyone tell me whats wrong with the code below;

I have 6 elements in my indexed array, the code displays the first 4 elements correctly, it then displays a number '2' or a number '6' for the 5th element (that's not in the array) and it displays the real 5th element in position 6 and the last element never gets echo'd!


$_SESSION['cart'][] = array(
1 => $style, $width, $height, $price, $position, $open);

foreach($_SESSION['cart'] as $cart_items)
{

for ($i = 0; $i < count($cart_items); $i++) {

echo $cart_items[$i];
}
}

alce

12:28 am on Jun 17, 2006 (gmt 0)

10+ Year Member



Try this:

$_SESSION['cart'] = array(
$style, $width, $height, $price, $position, $open);

foreach($_SESSION['cart'] as $cart_items){

echo $cart_items;
}

triangulum

7:35 am on Jun 17, 2006 (gmt 0)

10+ Year Member



Thanks alce,

I'd tried that, it doesn't work either. It displays all 6 elements but still get a '2' echo'd in the middle.

Any other suggestions, or does anyone know where this '2' is coming from?

dreamcatcher

10:46 pm on Jun 17, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



Try using:

print_r($_SESSION['cart']);

and see what that gives you.

dc

triangulum

8:34 am on Jun 18, 2006 (gmt 0)

10+ Year Member



The correct values are stored in the elements Dreamcatcher and print_r confirms it.

I've changed the code now to use an associative array to get it working but it would be nice to know where the foreach loop got that number from for future reference.

coopster

3:13 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Try echoing each variable in the array index (i.e. the $style, $width, $height, $price, $position, $open variables) separately to see what values they contain. Maybe even use var_dump() [php.net] to see not only the value but the type as well. Perhaps that will help identify the culprit.

triangulum

3:50 pm on Jun 19, 2006 (gmt 0)

10+ Year Member



Tried that too Coopster, all the variables contained the correct values.

The mystery continues!

coopster

5:57 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Which release of PHP are you running?

dreamcatcher

7:03 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Senior Member 10+ Year Member



it displays the real 5th element in position 6

Well, that would be correct as your first indice is 0. So, position 6 would be indice no.5.

dc

triangulum

7:29 pm on Jun 19, 2006 (gmt 0)

10+ Year Member



the number 'of' elements is 6.

1st element (0) $var correct value
2nd element (1) $var correct value
3rd element (2) $var correct value
4th element (3) $var correct value
5th element (4) echos the strange number "2"
6th element (5) echos the value of the variable stored at the 5th element (4).

The value of the 6th element (5) never gets echo'd

triangulum

7:30 pm on Jun 19, 2006 (gmt 0)

10+ Year Member



php version 2.8.0.2

coopster

7:35 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



Then I'd say you are a tad behind on your release level [php.net]!

jatar_k

10:29 pm on Jun 19, 2006 (gmt 0)

WebmasterWorld Administrator 10+ Year Member



when I first read that my brain automatically changed the 2 to a 4 (yes I know that makes no sense as there is no 4.8.0.2)

holy, you need that upgraded