Convert enumerated records to php object

Posted by Matt H on Stack Overflow See other posts from Stack Overflow or by Matt H
Published on 2010-04-05T22:54:25Z Indexed on 2010/04/05 23:23 UTC
Read the original article Hit count: 199

Filed under:
|

I have a table containing a bunch of records like this:

+-----------+--------+----------+
| extension | fwd_to | type     |
+-----------+--------+----------+
| 800       | 11111  | noanswer |
| 800       | 12345  | uncond   |
| 800       | 22222  | unavail  |
| 800       | 54321  | busy     |
| 801       | 123    | uncond   |
+-----------+--------+----------+

etc

The query looks like this:

select fwd_to, type from forwards where extension='800';

Now I get back an array containing objects which look like the following when printed with Kohana::debug:

(object) stdClass Object
(
    [fwd_to] => 11111
    [type] => noanswer
)

(object) stdClass Object
(
    [fwd_to] => 12345
    [type] => uncond
)

(object) stdClass Object
(
    [fwd_to] => 22222
    [type] => unavail
)

(object) stdClass Object
(
    [fwd_to] => 54321
    [type] => busy
)

What I'd like to do is convert this to an object of this form:

(object) stdClass Object
(
    [busy] => 54321
    [uncond] => 12345
    [unavail] => 22222
    [noanswer] => 11111
)

The reason being I want to then call json_encode on it. This will allow me to use jquery populate to populate a form.

Is there a suggested way I can do this nicely? I'm fairly new to PHP and I'm sure this is easy but it's eluding me at the moment.

© Stack Overflow or respective owner

Related posts about php

Related posts about kohana