Solved

Adding values to an array

Posted on 2003-11-08
8
307 Views
Last Modified: 2012-05-04
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","SomeItem2");

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.
0
Comment
Question by:drakkarnoir
8 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 250 total points
ID: 9708673
to add items to your array, use the following syntax:

$existing_array[] = "SomeItem3";

then when you're processing it, you can just do:

foreach($existing_array as $val) {

i suppose..

sessions can store arrays yes, i don't quite know what you're trying to do with that code though...

$array = array("my value", "my value2", "my value3");
$_SESSION["myarray"] = $array;

that's the syntax for adding an array to a session,

cheers,

loz
0
 

Author Comment

by:drakkarnoir
ID: 9708815
Is there another way to process it?
0
 

Author Comment

by:drakkarnoir
ID: 9708819
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
0
NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

 
LVL 4

Expert Comment

by:duerra
ID: 9709019
Yes:

for(x=0; x<count($array); x++)
0
 
LVL 33

Expert Comment

by:snoyes_jw
ID: 9709021
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++)
{
    ...
}

0
 
LVL 6

Expert Comment

by:aolXFT
ID: 9710214
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.
0
 
LVL 11

Assisted Solution

by:Zontar
Zontar earned 250 total points
ID: 9710326
For adding new items to an array you can also use

array_push($array, $newitem);
0
 

Author Comment

by:drakkarnoir
ID: 9716508
Thanks both of you! Been a great help :)
0

Featured Post

DevOps Toolchain Recommendations

Read this Gartner Research Note and discover how your IT organization can automate and optimize DevOps processes using a toolchain architecture.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit (http://en.wikipedia.org/wiki/PHPUnit) and similar technologies have enjoyed wide adoption, making it possib…
Foreword (July, 2015) Since I first wrote this article, years ago, a great many more people have begun using the internet.  They are coming online from every part of the globe, learning, reading, shopping and spending money at an ever-increasing ra…
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.
This tutorial will teach you the core code needed to finalize the addition of a watermark to your image. The viewer will use a small PHP class to learn and create a watermark.

777 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question