Welcome to WebmasterWorld Guest from 54.159.246.164

Forum Moderators: coopster & jatar k

Message Too Old, No Replies

Array being reset rather than added to

   
1:42 am on Mar 1, 2013 (gmt 0)



Hello,

I have some values that I am getting from a form users fill out. I am grabbing these values and storing them into sessions like so

$_SESSION['id'] = $_GET['id'];


Next, I'm storing those sessions into an array, like this

$array = array(
'model' => $_SESSION['model'],
'qty' => $_SESSION['qty'],
'id' => $_SESSION['id'],
);


I'm then trying to store that array, in another array. Like this

$other_array = Array(
"ID" => $array,
);



This works for the values that I submit from the form the first time around. When I submit different values from the same form I want it to create another array using the GET values and then store that in my $other_array.

Currently, it just replaces the first array that is added.

How can I get it to add another array rather than replacing it?
12:06 pm on Mar 2, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hi there algalogica, and a very warm welcom to WebmasterWorld!

Firstly, are you aware that $_SESSION is in it's very nature an array anyway, anything that has: ['somevalue'] is a reference to an element within an array.

If you did Print_r($_SESSION); you would see what I mean.

The way you're trying to do this would require multidimensional arrays, so that you could create a sort of look-up table for your sessions and the values.

Have a quick google for that and I'm sure that you'll find lots of tips on that.

Cheers,
(from a very tired)
MRb
6:24 pm on Mar 2, 2013 (gmt 0)

WebmasterWorld Senior Member 5+ Year Member



Hey Algalogica,

I'm reading this as thinking every time the user submits this form, it adds to an array of previously submitted data?

In that case, you don't need to specify a specific key for your $other_array. You can use a variable key, or just let it decide on keys by itself:

<?php

// Let it decide it's own key.
// First submission has key 0
// Next has key 1 etc..
// print_r($other_array[1]);
$other_array[] = $array;

// Or you can use something posted in the form
$other_array[$_GET['id']] = $array;

?>


Also, hi Matt! :)
11:44 pm on Mar 3, 2013 (gmt 0)



Thank you very much Matthew and Readie, these answers helped me solve my problem!