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 8147017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 6, 20262026-06-06T14:12:19+00:00 2026-06-06T14:12:19+00:00

I have a database table named Categories . I want to create a form

  • 0

I have a database table named Categories. I want to create a form dropdown containing all the categories in the db. My solution is to create an associative array and add it in the second parameter of the form_dropdown() function. My result is an unwanted multidimensional array.

Model:

function list_categories()
    {
        $user_id = $this->tank_auth->get_user_id();

        $this->db->where('user_id', $user_id);
        $this->db->order_by("date_created", "desc");
        $query = $this->db->get('categories');
        return $query;      
    }

View:

//create array
$categories = array();

if ($query->num_rows() > 0) 
{
     $categories = $query->result_array();
}

//create dropdown
echo form_dropdown('category', $categories, $date_created); //selected value based date created

The code gives a multidimensional array

Array ( 
[0] => Array ( [cat_id] => 27 [user_id] => 3 [cat_title] => Some Title [status] => 0 [date_created] => 2012-06-22 18:48:14 ) 

[1] => Array ( [cat_id] => 24 [user_id] => 3 [cat_title] => Title [status] => 0 [date_created] => 2012-06-20 19:37:47 ) 

[2] => Array ( [cat_id] => 23 [user_id] => 3 [cat_title] => Another Title [status] => 0 [date_created] => 2012-06-20 18:25:45 ) 

etc...

How can I replace the result above with an Associative Array where the ID key is the category id and value the category title?

Example:

$categories = array(
    "23" => "some title",
    "14" => "another title",
);
  • 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-06T14:12:20+00:00Added an answer on June 6, 2026 at 2:12 pm

    SOLUTION WITH AN EXAMPLE:

    $categories=array(
    '0'=>array("cat_id"=>"27","cat_title"=>"some title"),
    '1'=>array("cat_id"=>"24","cat_title"=>"Title"),
    '2'=>array("cat_id"=>"23","cat_title"=>"Another Title"),
    );
    
       foreach($categories as $catID=>$categoriesData){
            $finalArray[$categoriesData["cat_id"]]=$categoriesData;
       }
    
    print_r($finalArray);
    
    
    /*
    OUTPUT
    
    Array
    (
        [27] => Array
            (
                [cat_id] => 27
                [cat_title] => some title
            )
    
        [24] => Array
            (
                [cat_id] => 24
                [cat_title] => Title
            )
    
        [23] => Array
            (
                [cat_id] => 23
                [cat_title] => Another Title
            )
    
    )
    */
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have 2 database tables named Categories and SubCategories and I want to create
I have a simple database named customer with a single table data.I want to
I have a database with the following tables: create table Categories ( Id int
I have a database table named 'student' in which there is one column named
I have two columns(column1, column2) in my oracle database table named as demo .
I Have a DataBase in my project With Table named 'ProcessData' and columns named
I have a Database (SQL Server) with table named 'ProcessData' and columns named 'Process_Name'
We have a database where every table name starts with WW. I want to
In my database I have table AnimalDetails containing animalNumber(primary key),name,breed dateofbirth and an image
I have a database table, where I have single table to manage all root

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.