Merge - the result of merging into a single multidimensional array

I execute a simple query that joins two tables. I get something like this.

array(
    [0] => array(
        'id' => 52
        'name' => 'charles',
        'sale_id' => 921,
        'sale_time' => 1306393996,
        'sale_price' => 54.21
    ),
    [1] => array(
        'id' => 52
        'name' => 'charles',
        'sale_id' => 922,
        'sale_time' => 1306395000,
        'sale_price' => 32.41
    ),
    ...
);

... which is the expected result. However, I would like the request to return something like this:

array(
    [0] => array(
        'id' => 52,
        'name' => 'charles',
        'sales' => array(
            [0] => array(
                'sale_id' => 921,
                'sale_time' => 1306393996,
                'sale_price' => 54.21
            ),
            [1] => array(
                'sale_id' => 922,
                'sale_time' => 1306395000,
                'sale_price' => 32.41
            ),
            ...
        )
    )
)

Now I understand that I can simply fulfill two requests: one for user information and the other for sales, and combine these arrays together using any language that I use (in this case, PHP). But I have many arrays of properties, and the queries and merging for them seem awfully awkward for me (although this does work). It seems to me that it is possible to work with a single, unified object without duplicating data.

It’s just interesting if the query was without problems, or simply it’s just not easy via MySQL.

+3
source
1

, MySQL - . , , MySQL (PHP), - .

, ORM - Ruby ActiveRecord, Perl Class:: DBi, DBIx:: Class - PHP, , , .

+3

All Articles