user1149620
user1149620

Reputation: 873

Manipulating PHP Object Arrays

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.

Upvotes: 0

Views: 1022

Answers (2)

craig1231
craig1231

Reputation: 3867

To replace an object...

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

To remove from beginning of array use...

array_shift($a);

Upvotes: 4

Explosion Pills
Explosion Pills

Reputation: 191729

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

Upvotes: 3

Related Questions