0

I have a PHP object retrieved from MySQL, which is an array of objects, as below.

Array ( 
[0] => stdClass Object ( [question_id] => 1 [question_type] => multiple_choice [question_unit] => 7 [question_difficulty] => 56.5956047853 ) 
[1] => stdClass Object ( [question_id] => 2 [question_type] => multiple_choice [question_unit] => 7 [question_difficulty] => 54.665002232  ) 
[2] => stdClass Object ( [question_id] => 3 [question_type] => multiple_choice [question_unit] => 7 [question_difficulty] => 55.2923002984 ) 
)

I am trying to work out how I can replace object [0] with object [2], or remove object [0] and have the other objects indices decrease by 1. Is there a good/quick way of doing this, or do I just need to iterate through and overwrite it all manually?

Is there a tutorial on manipulating objects in PHP like this (I can do this for arrays quite simply, but can't find similar functions/resources for objects).

Thanks in advance.

1
  • 3
    an array of objects is still an array ;-) Commented Jun 6, 2012 at 22:21

2 Answers 2

4

To replace an object...

$a[0] = $a[2];

To remove from beginning of array use...

array_shift($a);
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks. Is there also a way to switch objects, so move one from spot [0] to spot [1]?
3

You can remove the first element of an array from the array with array_shift.

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.