Formatting stdClassObject for array

Asked

Viewed 46 times

3

I have a problem to convert some data into my system, I have the following code snippet that returns an array with a few objects:

Array(
    [4014] => stdClassObject([id] => 4014[registro] => 9877[sigla] => total)
    [4006] => stdClassObject([id] => 4006[registro] => 9877[sigla] => Parcial)
)

I need to convert these objects to an array to upload to a webservice in which the webservice pattern is described below:

Final Goal :

Array(
    [lista] => Array(
        [0] => Array(
            [registro] => 7069[sigla] => Parcial
        ) [1] => Array(
            [registro] => 7069[sigla] => Total
        ) 
    )
)

What I’ve been able to implement is this:

$dados = $DB->return_records($query);
foreach($dados as $dado) {
   $listagem['lista'][]= $dado;
}

And has returned the following excerpt:

Array(
    [lista] => Array(
                    [0] => stdClassObject([id] => 4014[registro] => 9877[sigla] => total)
                    [1] => stdClassObject([id] => 4006[registro] => 9877[sigla] => parcial)
                )
    )

Now I’m trying to generate the result of block 2, but I still can’t, someone could help me?

1 answer

5


Do it like this, you have to do the cast from each obj to array :

$dados = $DB->return_records($query);
foreach($dados as $dado) {
   $listagem['lista'][] = (array) $dado;
}
  • You’re boring in Miguel Rsrsrs, that’s all kkkkkk.

  • :p boring?! why? @tkmattos

  • Pq was very quick and surgeon in response. Brigadeo my dear

  • hehe de nada @tkmattos, I’m glad you solved . I’ve also been through this a lot, so know soon

  • 1

    It’s the evil of PHP KKKK typing

  • True, I don’t really like the syntax @tkmattos either

Show 1 more comment

Browser other questions tagged

You are not signed in. Login or sign up in order to post.