1

I am trying to insert an array into a 2-dimensional array at a specific position. According the manual, i should be able to do this with array_splice(), but it only deletes the contents of my receiving array without insertion.

I want to get an array with all the values (arrays) of $receivingArray plus the new value (array).

What am I doing wrong??

manual info:

array array_splice ( array &$input , int $offset [, int $length [, mixed $replacement = array() ]] )

If length is specified and is zero, no elements will be removed. If replacement is just one element it is not necessary to put array() around > it, unless the element is an array itself, an object or NULL.

input:

$newArray = array_splice($receivingArray, 0, 0, array($value));

result: $newArray is an empty array

input :

$newArray = array_splice($receivingArray, 1, 0, array($value));

result: $newArray is an empty array

this input:

print_r($receivingArray);
print_r(array($value));
$newArray = array_splice($receivingArray, 1, 1, array($value));
print_r($newArray);

gives: (interestingly)

Array
(
    [0] => Array
        (
            [id] => 1
            [primaryID] => 0
            [category_id] => 1
            [title] => sports
            [description] => 
            [selected] => 
            [level] => 0
        )

    [1] => Array
        (
            [id] => 4
            [primaryID] => 0
            [category_id] => 0
            [title] => programming
            [description] => 
            [selected] => 
            [level] => 0
        )

)
Array
(
    [0] => Array
        (
            [id] => 2
            [primaryID] => 1
            [category_id] => 1
            [title] => soccer
            [description] => 
            [selected] => 
            [level] => 1
        )

)
Array
(
    [0] => Array
        (
            [id] => 4
            [primaryID] => 0
            [category_id] => 0
            [title] => programming
            [description] => 
            [selected] => 
            [level] => 0
        )

)
3
  • 1
    First of all don't give this much array output . confusing. Just show initial $receivingArray. what you tried(code) and what your expected output. not whole array just a part of it which make sense to all of us. Commented May 29, 2015 at 2:37
  • ok, ill edit, thanks Commented May 29, 2015 at 3:08
  • you got the solution. no need to do now. thanks Commented May 29, 2015 at 3:08

2 Answers 2

4

From the docs for array_splice()

Return Values

Returns the array consisting of the extracted elements.

$newArray = array_splice($receivingArray, 0, 0, array($value));

array_splice modifies its input, so the results you're looking for are in $receivingArray and not $newArray

Sign up to request clarification or add additional context in comments.

Comments

0

I missed the fact that array_slice() doesn't actually return its output, but rather acts upon the receiving array itself, which is passed by reference. I didn't notice that there is an ammpersand in front of the first parameter in the manual specification.

this input:

print_r($receivingArray);
print_r(array($value));
array_splice($receivingArray, 0, 0, array($value));
print_r($receivingArray);

gives the correct result:

Array 
(
    [0] => Array
        (
            [id] => 1
            [primaryID] => 0
            [category_id] => 1
            [title] => sports
            [description] => 
            [selected] => 
            [level] => 0
        )

    [1] => Array
        (
            [id] => 4
            [primaryID] => 0
            [category_id] => 0
            [title] => programming
            [description] => 
            [selected] => 
            [level] => 0
        )

)
Array
(
    [0] => Array
        (
            [id] => 2
            [primaryID] => 1 
            [category_id] => 1
            [title] => soccer
            [description] => 
            [selected] => 
            [level] => 1
        )

)
Array
(
    [0] => Array
        (
            [id] => 1
            [primaryID] => 0
            [category_id] => 1
            [title] => sports
            [description] => 
            [selected] => 
            [level] => 0
        )

    [1] => Array
        (
            [id] => 2
            [primaryID] => 1
            [category_id] => 1
            [title] => soccer
            [description] => 
            [selected] => 
            [level] => 1
        )

    [2] => Array
        (
            [id] => 4
            [primaryID] => 0
            [category_id] => 0
            [title] => programming
            [description] => 
            [selected] => 
            [level] => 0
        )

)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.