[Okta Webinar] Learn how to a build a cloud-first strategyRegister Now

x
?
Solved

Adding values to an array

Posted on 2003-11-08
8
Medium Priority
?
316 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 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

How to Use the Help Bell

Need to boost the visibility of your question for solutions? Use the Experts Exchange Help Bell to confirm priority levels and contact subject-matter experts for question attention.  Check out this how-to article for more information.

Question has a verified solution.

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

Nothing in an HTTP request can be trusted, including HTTP headers and form data.  A form token is a tool that can be used to guard against request forgeries (CSRF).  This article shows an improved approach to form tokens, making it more difficult to…
Since pre-biblical times, humans have sought ways to keep secrets, and share the secrets selectively.  This article explores the ways PHP can be used to hide and encrypt information.
Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
The viewer will learn how to create and use a small PHP class to apply a watermark to an image. This video shows the viewer the setup for the PHP watermark as well as important coding language. Continue to Part 2 to learn the core code used in creat…
Suggested Courses
Course of the Month19 days, 3 hours left to enroll

834 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