?
Solved

Adding values to an array

Posted on 2003-11-08
8
Medium Priority
?
313 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
8 Comments
 
LVL 13

Accepted Solution

by:
lozloz earned 1000 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
WordPress Tutorial 3: Plugins, Themes, and Widgets

The three most common changes you will make to your website involve the look (themes), the functionality (plugins), and modular elements (widgets).

In this article we will briefly define each again, and give you directions on how to install them.

 
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 1000 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

Are You Using the Best Web Development Editor?

The worlds of web hosting and web development are constantly evolving. Every year we see design trends change, coding standards adapt and new frameworks/CMS created. With such a quick pace of change it’s easy to get lost trying to keep up.

See if your editor made the list.

Question has a verified solution.

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

3 proven steps to speed up Magento powered sites. The article focus is on optimizing time to first byte (TTFB), full page caching and configuring server for optimal performance.
There are times when I have encountered the need to decompress a response from a PHP request. This is how it's done, but you must have control of the request and you can set the Accept-Encoding header.
The viewer will learn how to count occurrences of each item in an array.
The viewer will learn how to create a basic form using some HTML5 and PHP for later processing. Set up your basic HTML file. Open your form tag and set the method and action attributes.: (CODE) Set up your first few inputs one for the name and …
Suggested Courses

752 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