Hello and welcome to our community! Is this your first visit?
Register
Enjoy an ad free experience by logging in. Not a member yet? Register.
Results 1 to 6 of 6
  1. #1
    Regular Coder
    Join Date
    Dec 2006
    Posts
    185
    Thanks
    17
    Thanked 0 Times in 0 Posts

    Inserting Values In Array

    Hi

    Does anyone know how to insert values into an array? For example i have an array called $array with orange green blue Black in that order how do i insert red in between green and blue?

    Thanks

  • #2
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,762
    Thanks
    19
    Thanked 155 Times in 146 Posts
    This is one way...I'm assuming your using numbered indices:
    PHP Code:
    <?

    $my_array 
    = array("orange","green","blue","black");

    foreach (
    $my_array as $val)
    {
        if (
    $val == "blue")
            
    $my_array2[] = "red";
        
    $my_array2[] = $val;
    }

    $my_array $my_array2;
    var_dump($my_array);

    ?>
    Last edited by chump2877; 03-08-2007 at 11:21 AM.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #3
    Super Moderator Inigoesdr's Avatar
    Join Date
    Mar 2007
    Location
    Florida, USA
    Posts
    3,642
    Thanks
    2
    Thanked 405 Times in 397 Posts
    This is another way you can do it:
    PHP Code:
    $search 'green';
    $after 'red';
    $my_array = array('orange','green','blue','black');
    array_splice($my_arrayarray_search($search$my_array), 1, array($search$after));
    var_dump($my_array); 

  • #4
    Regular Coder
    Join Date
    Dec 2006
    Posts
    185
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thanks for responding. Any idea as to how i can apply that to a user supplied number and when i get 3 place values it inserts a comma?

  • #5
    Senior Coder chump2877's Avatar
    Join Date
    Dec 2004
    Location
    the U.S. of freakin' A.
    Posts
    2,762
    Thanks
    19
    Thanked 155 Times in 146 Posts
    If I understand you right, instead of splitting up your multi-digit, user-supplied number into an array of one-digit numbers, just use number_format() to format your numbers.
    Regards, R.J.

    ---------------------------------------------------------

    Help spread the word! Like my YouTube-to-Mp3 Conversion Script on Facebook !! :)
    [Related videos and tutorials are also available at my YouTube channel and on Dailymotion]
    Get free updates about new software version releases, features, and bug fixes!

  • #6
    Regular Coder
    Join Date
    Dec 2006
    Posts
    185
    Thanks
    17
    Thanked 0 Times in 0 Posts
    Thats perfect!!! Thanks!!!


  •  

    Posting Permissions

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