#1
  1. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171

    Adding element ot the beginning of an array using array_unshift


    Hi;

    This is obviously not doing what I want it to do. I want to keep the array but the first 2 elements to be "select" and "d".

    What am I doing wrong? Thanks.
    PHP Code:
    print_r($templates);
    $templates array_unshift($templates,"select","d");  
    echo 
    "<hr />";
    print_r($templates); 
    Code:
    Array
    (
        [756] => bem
        [795] => eee
        [794] => Goosfand
        [793] => Khorram
        [796] => Sagaki
        [790] => Shashoo22
        [791] => Shashoo226
        [792] => Shashoo2261
    )
    <hr />10
  2. #2
  3. Did you steal it?
    Devshed Supreme Being (6500+ posts)

    Join Date
    Mar 2007
    Location
    Washington, USA
    Posts
    14,002
    Rep Power
    9398
    "Huh. $templates is now a number? That's weird: it's supposed to be an array. What happened?"

    ...

    "Okay, so I assigned whatever returned from array_unshift() to be $templates. What did it return? I should RTFM."


    array_unshift

    (PHP 4, PHP 5)
    array_unshift — Prepend one or more elements to the beginning of an array

    Description

    int array_unshift ( array &$array , mixed $value1 [, mixed $... ] )
    array_unshift() prepends passed elements to the front of the array. Note that the list of elements is prepended as a whole, so that the prepended elements stay in the same order. All numerical array keys will be modified to start counting from zero while literal keys won't be touched.
    "Why does it say int there? What int is it returning? And why is there an ampersand next to $array?"
    Return Values

    Returns the new number of elements in the array.
    "Oh. So how do I get the modified array? OH! That's what the ampersand means: $array is a reference so array_unshift() will modify the array I passed in. That means I just call the function and that's it!"
    PHP Code:
    print_r($templates); 
    array_unshift($templates,"select","d");   
    echo 
    "<hr />"
    print_r($templates); 
    Code:
    Array
    (
        [756] => bem
        [795] => eee
        [794] => Goosfand
        [793] => Khorram
        [796] => Sagaki
        [790] => Shashoo22
        [791] => Shashoo226
        [792] => Shashoo2261
    )
    <hr />Array
    (
        [0] => select
        [1] => d
        [2] => bem
        [3] => eee
        [4] => Goosfand
        [5] => Khorram
        [6] => Sagaki
        [7] => Shashoo22
        [8] => Shashoo226
        [9] => Shashoo2261
    )
    "Yay, that particular problem is solved! I'm so glad I looked at the documentation - it only took me a couple minutes to figure it out for myself! That's much faster than the half-hour I could expect to get on Dev Shed."



    Comments on this post

    • ManiacDan agrees : Go to sleep
  4. #3
  5. A Change of Season
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Mar 2004
    Location
    Next Door
    Posts
    2,653
    Rep Power
    171
    Hi;

    Thank you for this.

    My English is not that good. Specially my reading is bad. How is that for an excuse?

    Cheers
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Frequenter (2500 - 2999 posts)

    Join Date
    Dec 2004
    Posts
    2,972
    Rep Power
    375
    not very good, not even passable.. Reading is bad? yet Requix basically quoted the manual.. and you apparently read that perfectly...

    And you are from Australia not some 3rd world country
  8. #5
  9. Sarcky
    Devshed Supreme Being (6500+ posts)

    Join Date
    Oct 2006
    Location
    Pennsylvania, USA
    Posts
    10,866
    Rep Power
    6351
    Alright guys, let's settle down here.

    I doubt EBT was born in AUS, since...they speak English there.

    Regardless, Requinix's answer (of sorts) is correct and the problem is solved.
    HEY! YOU! Read the New User Guide and Forum Rules

    "They that can give up essential liberty to obtain a little temporary safety deserve neither liberty nor safety." -Benjamin Franklin

    "The greatest tragedy of this changing society is that people who never knew what it was like before will simply assume that this is the way things are supposed to be." -2600 Magazine, Fall 2002

    Think we're being rude? Maybe you asked a bad question or you're a Help Vampire. Trying to argue intelligently? Please read this.

IMN logo majestic logo threadwatch logo seochat tools logo