Solved

Adding values to an array

Posted on 2003-11-08
8
311 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 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
Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

 
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

PeopleSoft Has Never Been Easier

PeopleSoft Adoption Made Smooth & Simple!

On-The-Job Training Is made Intuitive & Easy With WalkMe's On-Screen Guidance Tool.  Claim Your Free WalkMe Account Now

Question has a verified solution.

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

This article discusses how to create an extensible mechanism for linked drop downs.
This article discusses how to implement server side field validation and display customized error messages to the client.
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.

738 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