PHP/MySQL   create simple array from table contents

Posted on 2010-01-01
Last Modified: 2013-12-12
Because i'm referencing this value translation vector many times in PHP code, i'd like to create an array based on the current contents of this small table, which (at present) contains


ID   Value
0    0                     (fwiw - a special case of default/unassigned)
1    1
2    3
3    5
4    10
5    11
6    20

(this is essentially an enumerated set of values)

what's the easiest way to dump these into an equivalent PHP array
array $CallType[] = (1,3,5,10,11,20)
Thus i can reference  $CallType [3]   and get the value  5
note that the IDs will always be contiguous and start at 0 - it's just that at some later date, more may be added, or the Values changed

is there a way to use  $Results  directly somehow?  (perhaps that's what $Results is?)

Question by:willsherwood
    LVL 11

    Accepted Solution

    $callType = array(
        array('0' => '0'),
        array('1' => '3'),
        array('2' => '5'),
        array('3' => '5'),
        array('4' => '10'),
        array('5' => '11'),
        array('6' => '20'),

    Open in new window


    Author Comment

    sorry i was not clear.  I want to automate extracting the current db values without hardcoding literals.

    is there an easier way than just   while ...     visiting each Result and creating each array entry one by one in a loop?
    LVL 11

    Assisted Solution

    How about something like:

    $result = mysql_query("SELECT myValue FROM myTable");
    echo '<pre>' . print_r(mysql_fetch_array($result,MYSQL_NUM),1) . '</pre>';

    Open in new window


    Write Comment

    Please enter a first name

    Please enter a last name

    We will never share this with anyone.

    Featured Post

    IT, Stop Being Called Into Every Meeting

    Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

    As this topic comes over and over again in different forms, I've finally decided to write a short (yea, right...) article / tutorial about pagination with PHP with MySQL database. There are dozens of these kind of tutorials, I know - I wanted to mak…
    Author Note: Since this E-E article was originally written, years ago, formal testing has come into common use in the world of PHP.  PHPUnit ( and similar technologies have enjoyed wide adoption, making it possib…
    Explain concepts important to validation of email addresses with regular expressions. Applies to most languages/tools that uses regular expressions. Consider email address RFCs: Look at HTML5 form input element (with type=email) regex pattern: T…
    The viewer will learn how to dynamically set the form action using jQuery.

    759 members asked questions and received personalized solutions in the past 7 days.

    Join the community of 500,000 technology professionals and ask your questions.

    Join & Ask a Question

    Need Help in Real-Time?

    Connect with top rated Experts

    13 Experts available now in Live!

    Get 1:1 Help Now