PHP (array_splice & array_slice)
From PHP.net
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.
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]]
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])
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
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:
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
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:
<?
$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:
array(2) {
[0]=>
string(1) "1"
[1]=>
string(1) "3"
}
greetings,
durandal
Oh well as long as it's working now. Thanks so much!
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