0

I can't use array_splice since my element is an object. Instead of inserting it as a whole, it inserts it field by field.

P.S.

I was inserting as Leigh said:

array_splice($original, 2, 0, $obj);

3
  • Can you elaborate on object? Can you just cast it using (array)? Commented Jul 16, 2012 at 11:39
  • @Martin I don't know what is "elaborate". I have a 'Page' class and an array of pages. I just want to take page from one position and put it into the other. Unbelieveable PHP couldn't do it! Commented Jul 16, 2012 at 11:43
  • @SuzanCioc please edit the original question and include your code :) Commented Jul 16, 2012 at 12:56

1 Answer 1

2

If you pass your object directly to array_splice you will get the behaviour you describe.

I think you are doing this:

$original = array(1, 2, 3, 4, 5);
$obj = new Object;

array_splice($original, 2, 0, $obj);

When you should be doing this:

array_splice($original, 2, 0, array($obj));

This way your object will be inserted as a whole, instead of the individual fields being inserted.

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

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.