#1
  1. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    59
    Rep Power
    15
    Ok, say I have an array, like

    $array[0] = "89 Chars";
    $array[1] = "82 Chars";

    But I want to insert and element between those two elements, so that it would be

    $array[0] = "89 Chars";
    $array[1] = "New Element";
    $array[2] = "82 Chars";

    I have looked through the php manual, and can't find a function to specifically do this, is there any, or do I kind of have to rig it?

    Chris
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    45
    Rep Power
    15
    There's no function to do what you want, however it's not that hard to do.Here's the code:

    $target = 1; //The index you want to insert the new value into
    $size = sizeof($array)+1;
    for($target;$target<$size;$target++)
    $array[$target+1] = $array[$target];
    $array[$target] = $value;

    Hope that helps
  4. #3
  5. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Posts
    59
    Rep Power
    15
    Ya, thats really close, thanks, I figured it out . I used array_slice instead though, that way I didn't mess up the order if there were more elements.

    Chris
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jul 2000
    Posts
    24
    Rep Power
    0
    <BLOCKQUOTE><font size="1" face="Verdana,Arial,Helvetica">quote:</font><HR>Originally posted by Chris Pickett:
    Ya, thats really close, thanks, I figured it out . I used array_slice instead though, that way I didn't mess up the order if there were more elements.

    Chris
    [/quote]

    wow, i posted a message suggesting array_splice, then decided to delete and change a couple of words, and in the mean time, you figured it out! nice! now i won't bother :P

    -weeder

Similar Threads

  1. Inserting checkbox values from an array into MySQL
    By outlyr242 in forum PHP Development
    Replies: 1
    Last Post: February 9th, 2004, 04:28 PM
  2. sort: find duplicate string elements in array
    By sciencelife in forum Java Help
    Replies: 3
    Last Post: January 27th, 2004, 01:29 PM
  3. multi dimensional arrays - Can't grasp how to?
    By Kaleina in forum PHP Development
    Replies: 17
    Last Post: November 5th, 2003, 03:18 PM
  4. Inserting array into mysql table
    By jackass in forum PHP Development
    Replies: 5
    Last Post: October 29th, 2003, 01:11 PM
  5. problem with array
    By Geekoidxp in forum PHP Development
    Replies: 3
    Last Post: October 12th, 2003, 01:22 PM

IMN logo majestic logo threadwatch logo seochat tools logo