Results 1 to 6 of 6

Thread: array_push syntax

  1. #1
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default array_push syntax

    here is the example from php.net:
    Code:
    <?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

    Code:
    <?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.
    Last edited by james438; 01-12-2010 at 02:41 AM.
    To choose the lesser of two evils is still to choose evil. My personal site

  2. #2
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    It should be:
    PHP Code:
    <?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.
    Jeremy | jfein.net

  3. #3
    Join Date
    Jan 2007
    Location
    Davenport, Iowa
    Posts
    2,385
    Thanks
    100
    Thanked 113 Times in 111 Posts

    Default

    Thanks. It looks like I am using the wrong function then. I am now using array_merge.
    To choose the lesser of two evils is still to choose evil. My personal site

  4. #4
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    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"
    Jeremy | jfein.net

  5. #5
    Join Date
    Mar 2006
    Location
    Illinois, USA
    Posts
    12,164
    Thanks
    265
    Thanked 690 Times in 678 Posts

    Default

    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.
    Daniel - Freelance Web Design | <?php?> | <html>| español | Deutsch | italiano | português | català | un peu de français | some knowledge of several other languages: I can sometimes help translate here on DD | Linguistics Forum

  6. #6
    Join Date
    Jan 2008
    Posts
    4,168
    Thanks
    28
    Thanked 628 Times in 624 Posts
    Blog Entries
    1

    Default

    @djr33
    I've discovered the same thing. Javacript should also have a '[]' suffix (is that the right word?).
    Jeremy | jfein.net

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •