0

Is it possible to make a select of some tables and push them into a Multidimensional array?

(i am using PDO fetch_all)

or do i have to make several selects ?

tables

TABLE xxx
 id | xxx | aaa | bbb | ccc
---------------------
  1 | 666 | 111 | 222 | 333
  2 | 777 | 444 | 555 | 666

TABLE positions
 xxx | yyy | zzz
----------------
 666 | 999 | 000
 666 | 888 | 111
 777 | 999 | 000
 777 | 888 | 222
 777 | 777 | 333
--^ //forenerkey from table xxx

What I want to achieve is

Array
(
    [0] => Array
        (
            [id] => 1
            [aaa] => 111
            [bbb] => 222
            [ccc] => 333
            [xxx] => 666
            [positions] => Array(
                           [0] => Array(
                                    [xxx] => 666
                                    [yyy] => 999
                                    [zzz] => 000
                                )
                           [1] => Array(
                                    [xxx] => 666
                                    [yyy] => 888
                                    [zzz] => 111
                                )
                         )

        )

    [1] => Array
        (
            [id] => 2
            [aaa] => 222
            [bbb] => 333
            [ccc] => 444
            [xxx] => 777
            [positions] => Array(
                           [0] => Array(
                                    [xxx] => 777
                                    [yyy] => 999
                                    [zzz] => 000
                                )
                           [1] => Array(
                                    [xxx] => 777
                                    [yyy] => 888
                                    [zzz] => 222
                                )
                           [2] => Array(
                                    [xxx] => 777
                                    [yyy] => 777
                                    [zzz] => 333
                                )
                         )
         )

)

Atm I select like this

select * from xxx x, position p where x.xxx=p.xxx

SQLFiddle

But the result is not as I like it. BTW, I don't even know how this should look like in mysql result ^^. Have to do a lot of afterwork to make the array like I want it in the end. Actually I have all done but I really would like to shrink my source :D

4
  • How does this comes? positions[1] => Array([xxx] => 777, [yyy] => 999, [zzz] => 000 ) Commented Aug 20, 2014 at 8:38
  • it doesn't, this is what i want to achieve :) atm i have severel selects and build this array with php. and this is just a sample array Commented Aug 20, 2014 at 8:40
  • But I think second array would be [666,888,111] instead of [777,999,000] Commented Aug 20, 2014 at 8:49
  • 1
    You are right, this was a typo. I have changed it. But as I sayed, its just a sample :) Commented Aug 20, 2014 at 8:52

1 Answer 1

3

You can have this alternative

Query

select
  x.*,
  p.xx as xpositions,
  p.yy as ypositions,
  p.zz as zpositions
from xxx x
  LEFT JOIN (SELECT
           xxx,
           GROUP_CONCAT(xxx) as xx,
           GROUP_CONCAT(yyy)    yy,
           GROUP_CONCAT(zzz)    zz
         FROM position
         group by xxx) p
    on p.xxx = x.xxx
GROUP BY x.xxx;

OUTPUT

| ID | XXX | AAA | BBB | CCC |  XPOSITIONS |  YPOSITIONS | ZPOSITIONS |
|----|-----|-----|-----|-----|-------------|-------------|------------|
|  1 | 666 | 111 | 222 | 333 |     666,666 |     999,888 |      0,111 |
|  2 | 777 | 444 | 555 | 666 | 777,777,777 | 999,888,777 |  0,222,333 |

Fiddle Demo

On php end you can handle it like this

foreach($rows as $row) {
    $ID     =   $row['ID'];
    $XXX    =   $row['XXX'];
    $BBB    =   $row['BBB'];
    $CCC    =   $row['CCC'];
    // explode with php
    $XPOSITIONS =   explode(',',$row['XPOSITIONS']);
    $YPOSITIONS =   explode(',',$row['YPOSITIONS']);
    $ZPOSITIONS =   explode(',',$row['ZPOSITIONS']);

    for($i=0;$i<count($XPOSITIONS);$i++){
        $x_position =   $XPOSITIONS[$i];
        $y_position =   $YPOSITIONS[$i];
        $z_position =   $ZPOSITIONS[$i];
    }
}
Sign up to request clarification or add additional context in comments.

1 Comment

this IS one select but i won't get around the php formating with this right ? because my source almost looks the same... actully my php is a lil bit shorter. and in the end i just want to shrink my source

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.