Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8830927
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 14, 20262026-06-14T08:12:03+00:00 2026-06-14T08:12:03+00:00

I’m hoping someone could help me with this problem. Say I have 3 DB

  • 0

I’m hoping someone could help me with this problem.

Say I have 3 DB tables:

Users:
user_id, user_name
100, John
101, Jessica

Cars:
car_id, car_name
30, Corvette
31, BMW


UsersCars:
user_id, car_id, car_colour
100, 30, Red
101, 30, Green
101, 31, Green

(so John got a red corvette and Jessica has a green Corvette and a BMW)

I would like to have code that returns a multidimensional PHP array something like:

Array
(
    [100] => Array
    (
        [user_id] => 100
        [user_name] => John
        [cars] => Array
        (
            [car_id]=>30,
            [car_name]=>'Corvette',
            [car_colour]=>'Red'

        )            
    )
    [101] => Array
    (
        [user_id] => 101
        [user_name] => Jessica
        [cars] => Array
        (
            [0] => Array
            (
                [car_id]=>30,
                [car_name]=>'Corvette',
                [car_colour]=>'Green'
            ),
            [1] => Array
            (
                [car_id]=>31,
                [car_name]=>'BMW',
                [car_colour]=>'Green'
            )
        )            
    )
)

I have the following SQL

SELECT u.*, c.* FROM Users u
LEFT JOIN UsersCars uc ON u.user_id = uc.user_id
LEFT JOIN Cars c ON uc.car_id = c.car_id

And PHP

$result = mysqli_query($db, $q);
while ($row = mysqli_fetch_assoc($result)) {
    $users_with_cars[$row['user_id']] = $row;
}

But this isn’t correct. Anyone knows how to solve this resulting the above array (with performance in mind)? I rather do not want to hard-coded the exception of “cars” being something that can happen more than once. I rather have something that just looks at $row and $users_with_cars and when seeing some new value, it appends it, by converting the old value into an array. Maybe there’s already a native PHP function for this? Or better, maybe my MySQL or whole approach is wrong?

Any help or tips appreciated.

Regards


UPDATE SOLVED

Here’s an update, maybe I can help someone else how I solved it eventually.

I ended up with always using an array for one or more cars, and I adjusted the tables to always have an “id” as column name. This way you can easily expand it. See example;

Users:
id, name
100, John
101, Jessica

Cars:
id, name
30, Corvette
31, BMW


UsersCars:
user_id, car_id, car_colour
100, 30, Red
101, 30, Green
101, 31, Green



$q = 'SELECT u.*, c.id as car_id, c.name as car_name, uc.colour as car_colour FROM Users u
LEFT JOIN UsersCars uc ON u.id = uc.user_id
LEFT JOIN Cars c ON uc.car_id = c.id';

$result = mysqli_query($db, $q);
while ($row = mysqli_fetch_assoc($result)) {
    $users_with_cars[] = $row;
}
$joins = array('cars' => array('car_id'=>'id','car_name'=>'name','car_colour'=>'colour'));
$users_with_cars = create_join_array($users_with_cars, $joins);

print_r($users_with_cars);



function create_join_array($rows, $joins){
    /* build associative multidimensional array with joined tables from query rows */

    foreach((array)$rows as $row){
        if (!isset($out[$row['id']])) {
            $out[$row['id']] = $row;
        }

        foreach($joins as $name => $item){
            unset($newitem);
            foreach($item as $field => $newfield){
                unset($out[$row['id']][$field]);
                if (!empty($row[$field]))
                    $newitem[$newfield] = $row[$field];
            }
            if (!empty($newitem))
                $out[$row['id']][$name][$newitem[key($newitem)]] = $newitem;
        }
    }

    return $out;
}

This all results in the beautiful array:

Array
(
    [100] => Array
    (
        [id] => 100
        [name] => John
        [cars] => Array
        (
            [30] => Array
            (
                [id]=>30
                [name]=>'Corvette',
                [colour]=>'Red'
            )
        )            
    )
    [101] => Array
    (
        [id] => 101
        [name] => Jessica
        [cars] => Array
        (
            [30] => Array
            (
                [id]=>30,
                [name]=>'Corvette',
                [colour]=>'Green'
            ),
            [31] => Array
            (
                [id]=>31,
                [name]=>'BMW',
                [colour]=>'Green'
            )
        )            
    )
)

Let’s say the users also can have multiple bikes. Then, you have multiple join arrays, you can easily bind on with left joins and add it to the join array.

$q = 'SELECT u.*, c.id as car_id, c.name as car_name, uc.colour as car_colour, b.id as bike_id, b.name as bike_name FROM Users u
LEFT JOIN UsersCars uc ON u.user_id = uc.user_id
LEFT JOIN Cars c ON uc.car_id = c.id
LEFT JOIN UsersBikes ub ON u.user_id = ub.user_id
LEFT JOIN Bikes b ON ub.bike_id = b.id';

$result = mysqli_query($db, $q);
while ($row = mysqli_fetch_assoc($result)) {
    $users_with_cars_bikes[] = $row;
}

$joins = array('cars' => array('car_id'=>'id', 'car_name'=>'name', 'car_colour'=>'colour'), 
               'bikes' => array('bike_id'=>'id', 'bike_name'=>'name'));
$users_with_cars_bikes = create_join_array($users_with_cars_bikes, $joins);

print_r($users_with_cars_bikes);

Would result in something like

Array(
    [100] => Array
    (
        [id] => 100
        [name] => John
        [cars] => Array
        (
            [30] => Array
            (
                [id]=>30
                [name]=>'Corvette',
                [colour]=>'Red'
            )
        )
        [bikes] => Array
        (
            [41] => Array
            (
                [id]=>41
                [name]=>'BMX'
            )
        )           
    )
)

and so on..

Thanks all for helping out 🙂

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-14T08:12:05+00:00Added an answer on June 14, 2026 at 8:12 am

    This is the thing I could come up with. It will (probably) create an array just like your desired output. Not sure if it will work, I wrote this here without testing. Let me know! 🙂

    $result = mysqli_query($db, $q);
    while ($row = mysqli_fetch_assoc($result)) 
    {
        // Add user ID and name to the array
        $users_with_cars[$row['user_id']]['user_id'] = $row['user_id'];
        $users_with_cars[$row['user_id']]['user_name'] = $row['user_name'];
        // Check if this user has cars in the array. If not, this is the first car (see else)
        if(isset($users_with_cars[$row['user_id']]['cars']))
        {
            // Check if there is exactly 1 car in the array
            if(count($users_with_cars[$row['user_id']]['cars']) == 1)
            {
                // If yes, put that car in a 'sub array'
                $users_with_cars[$row['user_id']]['cars'] = array(0 => $users_with_cars[$row['user_id']]['cars']);
                // Then add the new car
                $users_with_cars[$row['user_id']]['cars'][] = array('car_id' => $row['car_id'], 'car_name' => $row['car_name'], 'car_color' => $row['car_color']);
            }
            else
            {
                // It already has more than one car in the array. Just add it
                $users_with_cars[$row['user_id']]['cars'][] = array('car_id' => $row['car_id'], 'car_name' => $row['car_name'], 'car_color' => $row['car_color']);
            }
        }
        else
        {
            // Add a single car without 'sub array'
            $users_with_cars[$row['user_id']]['cars']['car_id'] = $row['car_id'];
            $users_with_cars[$row['user_id']]['cars']['car_name'] = $row['car_name'];
            $users_with_cars[$row['user_id']]['cars']['car_color'] = $row['car_color'];
        }
    }
    

    EDIT:

    This is an example as I mentioned in my comment:

    $result = mysqli_query($db, $q);
    while ($row = mysqli_fetch_assoc($result)) 
    {
        $count = count($users_with_cars[$row['user_id']]['cars']);
        foreach($row AS $key => $value)
        {
            if(substr($key, 0, 4) == "car_")
            {
                // Single:
                $users_with_cars[$row['user_id']]['cars'][$key] = $value;
                // Multiple:
                $users_with_cars[$row['user_id']]['cars'][$count][$key] = $value;
            }
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a string like this: La Torre Eiffel paragonata all’Everest What PHP function
This could be a duplicate question, but I have no idea what search terms
this is what i have right now Drawing an RSS feed into the php,
I have this code to decode numeric html entities to the UTF8 equivalent character.
I'm parsing an RSS feed that has an ’ in it. SimpleXML turns this
I have this code: - (void)parser:(NSXMLParser *)parser foundCDATA:(NSData *)CDATABlock { NSString *someString = [[NSString
Let's say I'm outputting a post title and in our database, it's Hello Y’all
I have a .ini file as follows: [playlist] numberofentries=2 File1=http://87.230.82.17:80 Title1=(#1 - 365/1400) Example
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I have just tried to save a simple *.rtf file with some websites and

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.