0

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.

2
  • The elements of that result array are objects. Just as the dump clearly states. The question is what you tried to convert here, certainly not an object in the first place. No object would result in such a numeric array when converted. Also I fail to see how you want to receive back an object from a plain sql query. So please, add some more details: your query, the processing lines and a dump of the original "object". Commented Feb 13, 2022 at 20:22
  • The best solution would be to retrieve an array of arrays from the database, rather then getting an array of objects from the database. Most db drivers I have seen allow you to fetch arrays Commented Feb 13, 2022 at 21:29

1 Answer 1

1

To covert the object to an array you should change

$arr = (array)$object_name;

To this instead

$arr = json_decode($object_name, true);

Read more https://www.php.net/manual/en/function.json-decode.php

Sign up to request clarification or add additional context in comments.

1 Comment

The final approach I took, was to rewrite the sql query so that it returned and array, that I could then filter through. I then made the array an object so it was easier to then manage for view template purposes. Probably not best practice, but am still learning and my app is working whilst only making one db query. Thanks for the help.

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.