• Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 218
  • Last Modified:

PHP/MySQL create simple array from table contents

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?)

  • 2
2 Solutions
$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

willsherwoodAuthor Commented:
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?
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


Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now