I have a PHP object retrieved from MySQL.
I can retrieve the values for each object by going through a foreach loop and using the => operator. What I would like to do is apply some logic prior to the foreach loop and potentially to edit/delete some of the rows.
How would I go about changing the value of one of the items, for example question_id for object 1? Also how would I loop through each object and delete it if a certain condition is met? How do I then insert a new object?
Is there a tutorial somewhere that I can read, I can do most stuff with arrays but objects are new to 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():
http://php.net/manual/en/function.unset.php
If you want to loop through the object and manipulate it, you can use foreach() and pass the
$objby reference using&.For example, if you want to remove all
question_idkeys if it’s equal to3, use this:Alternatively, if you want to remove the whole object, you could do it like this:
Update
It seems passing by reference does not work (at least not in your case), so you could try this: