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

  • Home
  • SEARCH
  • 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 4026358
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 20, 20262026-05-20T10:57:37+00:00 2026-05-20T10:57:37+00:00

need to return an array like this for example: array(30 => ‘Mercedes Benz 310

  • 0

need to return an array like this for example:

 array(30 => 'Mercedes Benz 310 ',26 => 'Lamborghini Murcielago')

I have a database set up something like this:

CREATE TABLE cars (
  id bigint(20) NOT NULL auto_increment,
  `car_name` tinyint(2) NOT NULL default '0',
  owner varchar(20) NOT NULL default ''
  PRIMARY KEY  (id)
) ENGINE=MyISAM; 

The id need to be the array key.

So I tried to use foreach, but I have still not quite understood how it works.

$q = "select `id`, `car_name` from `cars` where `owner`='$username'";
$result = $conn->query($q); 
unset($q); 

if( !$result){
    return array(0 => 'error'); 
}

$garage = $result->fetch_assoc(); 
$car_id = $garage["id"];
$car_name = $garage["car_name"]; 



foreach( $car_name as $key => $car_id ){

    ...

}
  • 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-05-20T10:57:38+00:00Added an answer on May 20, 2026 at 10:57 am

    You aren’t far off. Something like this should give you the kind of array you’re looking for.

        $q = "select `id`, `car_name` from `cars` where `owner`='$username'";
        $result = $conn->query($q); 
        unset($q); 
    
        if( !$result){
            return array(0 => 'error'); 
        }
    
        while($row = mysql_fetch_array($result)){
          $garage[$row['id']] = $row['car_name'];
        }
    
    return $garage;
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I currently have an array set up like this: $u_id= array( array( NUM=>'2770', DESC=>'description
For example, I have JavaScript array like this: var data = [ {text:'Business', aid:'6'},
I have a multidimensional array that looks something like this: ourThing = array( 'id'
I essentially have an array of values like this: 0.25, 0.24, 0.27, 0.26, 0.29,
I have a binary, the database table of relationships looks like this: +----+----------+---------+-----+ |
I have an array of strings that looks like this: [Greater New York City
I have an array of folders/files that looks like this: Array ( [Root Folder
I have a 2-D numpy array with 100,000+ rows. I need to return a
I have a string like this, sidePanel[]=1&sidePanel[]=2&sidePanel[]=4 And if I need to add another
I have a value like this: $id = $_GET['id']; I need to compare this

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.