drakkarnoir
asked on
Adding values to an array
I'm making a little shopping cart using arrays and sessions...
I was wondering, how would I add an item to an existing array of things when the user clicks "Add to cart". Meaning, I have the page setup down ok, but like if I have:
$existing_array = array("SomeItem","SomeItem 2");
How would I tell PHP to add "SomeItem3" into the $existing_array? I guess what I'm asking is...how do I add elements to an array as I go along? And also, sessions can store arrays correct? So I could do the following:
$_SESSION[arrayname] = $initialized_array
$existing_array = array();
Is that ok? Or do I need to change my logic?
Thank you much in advance.
I was wondering, how would I add an item to an existing array of things when the user clicks "Add to cart". Meaning, I have the page setup down ok, but like if I have:
$existing_array = array("SomeItem","SomeItem
How would I tell PHP to add "SomeItem3" into the $existing_array? I guess what I'm asking is...how do I add elements to an array as I go along? And also, sessions can store arrays correct? So I could do the following:
$_SESSION[arrayname] = $initialized_array
$existing_array = array();
Is that ok? Or do I need to change my logic?
Thank you much in advance.
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Like can I do:
for(x=0;x>array.length();x ++)
{
echo "You select these items: <br> $existing_array[x]";
}
?
Sorry I'm a C++ guy hehe
for(x=0;x>array.length();x
{
echo "You select these items: <br> $existing_array[x]";
}
?
Sorry I'm a C++ guy hehe
Yes:
for(x=0; x<count($array); x++)
for(x=0; x<count($array); x++)
probably the easiest way to run through it is as lozloz suggests, e.g.
foreach ($existing_array as $val)
echo "<br/>$val";
You can do what you wish, e.g.
for ($x = 0; $x < count($existing_array); $x++)
{
...
}
foreach ($existing_array as $val)
echo "<br/>$val";
You can do what you wish, e.g.
for ($x = 0; $x < count($existing_array); $x++)
{
...
}
Since you are a C++ Guy I'm sure efficiency is important to you :)
$count = count($array);
for($x = 0; $x < $count; ++$x){
//----
}
This doesn't answer your question in anyway(so please don't accept it as an answer), but you'll notice that I've pre-incremented the $x(approx 10% faster than post-inc), and stored the count in a variable.
Bare in mind, that that will only work for enumerated arrays, and not assoc arrays.
Having all that said. I really recommend that when you are using PHP, that you do things the PHP way, and use foreach like lozloz suggested.
$count = count($array);
for($x = 0; $x < $count; ++$x){
//----
}
This doesn't answer your question in anyway(so please don't accept it as an answer), but you'll notice that I've pre-incremented the $x(approx 10% faster than post-inc), and stored the count in a variable.
Bare in mind, that that will only work for enumerated arrays, and not assoc arrays.
Having all that said. I really recommend that when you are using PHP, that you do things the PHP way, and use foreach like lozloz suggested.
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Thanks both of you! Been a great help :)
ASKER