PHP (array_splice & array_slice)

Posted:
in Genius Bar edited January 2014
From PHP.net



Quote:

array_splice -- Remove a portion of the array and replace it with something else



array array_splice ( array input, int offset [, int length [, array replacement]]



Quote:

array_slice -- Extract a slice of the array



array array_slice ( array array, int offset [, int length])







Correct me if I'm wrong pretty please, but shouldn't this return what is located at $UserName($key)?



$UserName = array_slice($UserName, $key, 1);





Again, correct me if I'm wrong, but shouldn't this return everything BUT what is at $UserName($key)?



$UserName = array_splice($UserName, $key, 1);





It's driving me absolutly crazy, and to tell you the truth, I've used both and they both give me the same thing. NOTHING is more frustrating when things like this happen.

Comments

  • Reply 1 of 5
    durandaldurandal Posts: 277member
    Quote:

    Originally posted by ast3r3x



    Again, correct me if I'm wrong, but shouldn't this return everything BUT what is at $UserName($key)?



    $UserName = array_splice($UserName, $key, 1);





    Nope. The function works exactly as advertised. From the array_splice description at php.net:



    Quote:

    It returns an array containing the extracted elements.



    Your call to array_slice returns element $key from $UserName, and array_splice extracts that element. Therefore the results you get are correct.



    greetings,

    durandal
  • Reply 2 of 5
    ast3r3xast3r3x Posts: 5,012member
    So if I want an array of everything but $key from the array $UserName, how would I do that?
  • Reply 3 of 5
    durandaldurandal Posts: 277member
    Quote:

    Originally posted by ast3r3x

    So if I want an array of everything but $key from the array $UserName, how would I do that?



    Like this:



    Code:




    <?

    $UserName = array("1","2","3");

    $key = 1;



    array_splice($UserName, $key, 1);

    var_dump($UserName);

    ?>









    array_splice extracts the specified number of elements from offset $key out of $UserName. Therefore $UserName will contain everything but the element at position $key after "splicing" the array.



    var_dump is a function that spits out the contents of any given variable. In the above function it returns:



    Code:




    array(2) {

    [0]=>

    string(1) "1"

    [1]=>

    string(1) "3"

    }









    greetings,

    durandal
  • Reply 4 of 5
    ast3r3xast3r3x Posts: 5,012member
    I think I'm going to cry. I swear I was doing that before and it wasn't working.



    Oh well as long as it's working now. Thanks so much!
  • Reply 5 of 5
    durandaldurandal Posts: 277member
    Quote:

    Originally posted by ast3r3x

    I think I'm going to cry. I swear I was doing that before and it wasn't working.



    Oh well as long as it's working now. Thanks so much!




    No problemo nice sig, btw



    greetings,

    durandal
Sign In or Register to comment.