Solved

Adding values to an array

Posted on 2003-11-08
8
305 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
 
LVL 4

Expert Comment

by:duerra
ID: 9709019
Yes:

for(x=0; x<count($array); x++)
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
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

Highfive Gives IT Their Time Back

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

This article will explain how to display the first page of your Microsoft Word documents (e.g. .doc, .docx, etc...) as images in a web page programatically. I have scoured the web on a way to do this unsuccessfully. The goal is to produce something …
I imagine that there are some, like me, who require a way of getting currency exchange rates for implementation in web project from time to time, so I thought I would share a solution that I have developed for this purpose. It turns out that Yaho…
The viewer will learn how to dynamically set the form action using jQuery.
The viewer will learn how to look for a specific file type in a local or remote server directory using PHP.

746 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now