Create array from mysql query. Column2 as key Column 1 as value

mysql, php

Solution

Note Your question title is inconsistent with your example. In the title you ask for column 1 as the key, but your example uses column 2 as the key. I've used column 2 here...

It isn't clear what MySQL API you are using to fetch, but whichever it is, use the associative fetch method and create new array keys using the pattern `$arraytable[$newkey] = $newvalue`. This example would be in object-oriented MySQLi:

$arraytable = array();

while($row = $result->fetch_assoc()) {
  // Create a new array key with the 'link' and assign the 'state'
  $arraytable[$row['link']] = $row['state'];
}

Problem

I have a table that contains ``` column 1 = state column 2 = link Alabama auburn.alabama.com Alabama bham.alabama.com Alabama dothan.alabama.com ``` I need to grab from my database table and put into an array that i can array_walk() through. they need to be accessed like this array. ``` $arraytable = array( "auburn.alabama.com"=>"Alabama", "bham.alabama.com"=>"Alabama", "dothan.alabama.com"=>"Alabama", ); ``` I have tried everything but not sure how make this work using php to print the array like such. Any help is greatly appreciated.

Original source