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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T11:12:43+00:00 2026-06-17T11:12:43+00:00

I have an Array of product types in PHP, it looks pretty much like

  • 0

I have an Array of product types in PHP, it looks pretty much like this:

$types = [
    0 => "cars",
    1 => "motorbikes",
    2 => "boats",
    3 => "airplanes"
];

So that when then user wants to get or save it, I can use the ID of the category to insert or get it from the database, like this:

select * from items where type = 0;

now, My doubt is the following: if I am using integer index keys, would it not be the same to use the following?

$types = ["cars", "motorbikes", "etc."];

Since PHP will give an integer auto-ordering.

Which one would you consider to be best practice in this case? The advantage of the first example seems to only be the fact that I can assign different keys, like for private categories or such, but I don’t know really.

It is also important to consider that the values will need to be translated so, should I consider even just using IDs? like

$types = ["t1", "t2", "etc."];

and then insert the translation somewhere else?

  • 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-17T11:12:44+00:00Added an answer on June 17, 2026 at 11:12 am

    There are 2 possible options.

    1. Your current option 1 with manually set indexes.
    2. A way better one – a table in the DB, holding these categories, which allow editing categories without losing the association between keys and names.

    Your current option 2 is not an option at all – it will break the order the same moment you insert or delete a category

    Here you set the index:

        $types = [
            3 => "airplanes"
      index ^         ^ value
    

    So, if you delete cars from the list, the index remain associated with it’s value:

    $types = [
        1 => "motorbikes",
        2 => "boats",
        3 => "airplanes"
    ];
    

    While if you don’t set indexes manually

    $types = ["motorbikes", "boats", "airplanes"];
    

    airplanes will lose it’s association, and become boats. It’s not ht magic we expect from the web application

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an ajax request that looks like this, $(#frmProducts).submit(function(){ var dataSet = $(#frmProducts).serialize();
I currently have two arrays that look like this: Swatches: Array ( [0] =>
I have my form like this: Product dropdown Quantity text field UnitPrice display TotalPrice
I have a form that looks like the following: <input type=text name=item[101][0][date] value=2012 />
I have a table that looks like: +--------------------+-------------+------+-----+---------+-------+ | Field | Type | Null
For eg I have this code on the main page. <?php session_start(); $_SESSION['order']=array(); ?>
I have a object (product), with a property of type 'array' e.g. product.tags =
I have some Javascript code that creates 2 arrays: One for Product Category and
Howdy. It's me again, and It's jQuery again. I have somthing like that: http://misiur.com/small/
For example I have 3 entities: Category Subcategory Product In SonataAdminBundle I'd like to

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.