arrays - Manipulating PHP Objects (Deleting/Editing) -


i have php object retrieved mysql.

i can retrieve values each object going through foreach loop , using => operator. apply logic prior foreach loop , potentially edit/delete of rows.

how go changing value of 1 of items, example question_id object 1? how loop through each object , delete if condition met? how insert new object?

is there tutorial somewhere can read, can stuff arrays objects new me.

thanks in advance.

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      )  ) 

you can delete object properties using unset():

it possible unset object properties visible in current context.

http://php.net/manual/en/function.unset.php

unset($array[1]->question_type); 

if want loop through object , manipulate it, can use foreach() , pass $obj reference using &.

for example, if want remove question_id keys if it's equal 3, use this:

foreach ($array &$obj) {     if ($obj->question_id == 3) unset($obj->question_id); } 

alternatively, if want remove whole object, this:

foreach ($array &$obj) {     if ($obj->question_id == 3) unset($obj); } 

update

it seems passing reference not work (at least not in case), try this:

$c = count($questions); ($i=0; $i<$c; $i++) {     if ($questions[$i]->question_id == 3) unset($questions[$i]); } 

Comments

Popular posts from this blog

java - Play! framework 2.0: How to display multiple image? -

gmail - Is there any documentation for read-only access to the Google Contacts API? -

php - Controller/JToolBar not working in Joomla 2.5 -