简体   繁体   中英

Build PHP array from query

I'm currently manually building an array.

$this->array_list = array('Alaska' => 'Alaska',
                          'Amanda' => 'Amanda',
                          'America' => 'America',
                          'Anthea' => 'Anthea',
                          'Arena' => 'Arena',
                          'Atlantis' => 'Atlantis'
                         );

I have a database that i can query this list from, My question is how do I create the array with key and value once getting the list from the query? Key and Value will always be the same. Using mysql and codeigniter.

Query DB for list, create two arrays and use combine? Has to be an easier way?

Here is what I ended up doing. Probably not the best way, feel free to give better way to do this.

$sofa_list2 = $this->get_media_model->get_sofas();
            foreach ( $sofa_list2 as $k=>$v )
                {
                  $newkey = $sofa_list2[$k]['collection_name'];
                  $sofa_list2[$k] ["$newkey"] = $sofa_list2[$k] ['collection_name'];
                  unset($sofa_list2[$k]['collection_name']);
                }
 $sofa_list2 = call_user_func_array('array_merge', $sofa_list2);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM