I have written a sql query which returns an object. Having used the object in my view file, I now need to convert it to an array so that I can remove the duplicate entries.
To covert the object to an array, I am using,
$arr = (array)$object_name;
Here is the result.
Array
(
[0] => stdClass Object
(
[id] => 1
[body_parts_id] => 2
[conditions_id] => 1
[priorities_id] => 1
[name_of_body_part] => Lungs and airways
[name_of_condition] => Unconscious, not breathing or responding.
[priority_title] => Priority One
)
[1] => stdClass Object
(
[id] => 1
[body_parts_id] => 1
[conditions_id] => 1
[priorities_id] => 1
[name_of_body_part] => Heart and blood vessels
[name_of_condition] => Unconscious, not breathing or responding.
[priority_title] => Priority One
)
[2] => stdClass Object
(
[id] => 1
[body_parts_id] => 1
[conditions_id] => 2
[priorities_id] => 1
[name_of_body_part] => Heart and blood vessels
[name_of_condition] => Signs of a heart attack
[priority_title] => Priority One
)
[3] => stdClass Object
(
[id] => 1
[body_parts_id] => 1
[conditions_id] => 3
[priorities_id] => 1
[name_of_body_part] => Heart and blood vessels
[name_of_condition] => Heavy bleeding that won't stop
[priority_title] => Priority One
)
[4] => stdClass Object
(
[id] => 2
[body_parts_id] => 3
[conditions_id] => 4
[priorities_id] => 2
[name_of_body_part] => Muscle, bone and joint
[name_of_condition] => Condition test 1
[priority_title] => Priority Two
)
)
The problem comes when I try and now loop through the array to remove any duplication. I get the message.
Cannot use object of type stdClass as array
Clearly I am not converting the Object to an Array correctly.
Can someone help please - thank you.