Log in

View Full Version : Resolved array_push syntax



james438
01-12-2010, 01:11 AM
here is the example from php.net:

<?php
$stack = array("orange", "banana");
array_push($stack, "apple", "raspberry");
print_r($stack);
?>
What I want is to put the variables "apple" and "raspberry" into a variable or array as in


<?php
$fruit2="apple,raspberry";
$stack = array("orange", "banana");
array_push($stack, $fruit2);
print_r($stack);
?>
While the above works I am not sure as to what the correct syntax should be. Does this look correct? Somehow I figure there should be escaped quotes in $fruit2.

Nile
01-12-2010, 02:06 AM
It should be:


<?php
$fruit_a = "apple";
$fruit_b = "raspberry";
$stack = array("orange", "banana");
array_push($stack, $fruit_a, $fruit_b);
print_r($stack);
?>

Or else apple and rasperry is one value.

james438
01-12-2010, 02:22 AM
Thanks. It looks like I am using the wrong function then. I am now using array_merge.

Nile
01-12-2010, 02:36 AM
Ok.

It seems your topic is solved... Please set the status to resolved.. To do this:
Go to your first post ->
Edit your first post ->
Click "Go Advanced" ->
Then in the drop down next to the title, select "RESOLVED"

djr33
01-12-2010, 04:00 AM
Array_push is all but useless: it is just like $array[] = ...

Maybe it would be shorter syntax for many variables but that is about all.

Nile
01-12-2010, 04:05 AM
@djr33
I've discovered the same thing. Javacript should also have a '[]' suffix (is that the right word?).