Jump to content

Recommended Posts

I've got the following two tables and wish to create the following array.  What is the best way to create the array?  Thanks

  1. Do the first query and loop through them using PHP, and then a prepared statement for the second?
  2. Inner join the two tables, loop through them using PHP, and set an array if not currently set, and if set, just add to the array?
  3. Inner join the two tables, and use GROUP_CONCAT and then explode()?  While I think this approach is slick, I don't want to use it due to the truncation limitation.
  4. Something else?

 

Companies

1 Ford
3 General Motors
5 Chrysler

Cars

1 1 Mustang
2 1 Fusion
3 3 Corvette
4 3 Escalade
5 5 Charger
6 5 Challenger
$cars=[
    [ 'id'=>1, 'name'=>'Ford', 'cars'=>[ ['id'=>1, 'Mustang'], ['id'=>2, 'Fusion'] ] ],
    [ 'id'=>3, 'name'=>'General Motors', 'cars'=>[ ['id'=>3, 'Corvette'], ['id'=>4, 'Escalade'] ] ],
    [ 'id'=>5, 'name'=>'Chrysler', 'cars'=>[ ['id'=>5, 'Charger'], ['id'=>6, 'Challenger'] ] ]
];

try

$sql = "SELECT co.id
        , co.name
        , car.id
        , car.model
        FROM companytest co
            INNER JOIN cartest car ON co.id = car.make
        ORDER BY co.id, car.id";
$res = $db->query($sql);
$cars = [];
$prev = '';
while (list($coid, $coname, $cid, $model) = $res->fetch_row()) {
    if (!isset($cars[$coid])) {
        $cars[$coid] = ['id' => $coid, 'name' => $coname, 'cars' =>[] ];
    }
    $cars[$coid]['cars'][] = ['id' => $cid, 'model' => $model];
}

the data

mysql> select * from companytest;
+------+----------------+
| id   | name           |
+------+----------------+
|    1 | Ford           |
|    3 | General Motors |
|    5 | Chrysler       |
+------+----------------+
3 rows in set (0.00 sec)

mysql> select * from cartest;
+------+------+------------+
| id   | make | model      |
+------+------+------------+
|    1 |    1 | Mustang    |
|    2 |    1 | Fusion     |
|    3 |    3 | Corvette   |
|    4 |    3 | Escalade   |
|    5 |    5 | Charger    |
|    6 |    5 | Challenger |
+------+------+------------+
6 rows in set (0.00 sec)
Edited by Barand

@requinix - It was tricky getting the timing just right :)

"Oh I see he's reading the topic. I know he'll reply to this so I'll just wait a bit... (refresh) No, he hasn't replied yet. I'll give him more time... (refresh) Still no... (refresh) Alright, it's been an hour, I have this reply ready so I'll just copy and paste it in here, and voila! there's my po-- DAMMIT"

 

context? I posted a reply just after Barand saying the same thing so I hid mine

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.