I am trying to assign my db
returned results to an array
I have
$results = Db::get('get_items', array('id' =>$id));
$Info['items'][1]['title'] = $results[0]['Name'];
$Info['items'][1]['name'] = $results[0]['Filename'];
$Info['items'][1]['type'] = $results[0]['Type];
The DB only returns 1 row of data.
I need my $info
looks like the following
Array
(
[items] => Array
(
[1] => Array
(
[title] => Title test
[filename] => test.xml
[type] => company
)
)
[mis] => data
)
I was wondering if there are a better way to assign the value to $info
than just hardcoded
them all. Thanks a lot!
Use an array to describe the key mappings:
$map = array('Name' => 'title',
'Filename' => 'name',
'Type' => 'type');
foreach ($results[0] as $key => $value) {
$Info['items'][1][$map[$key]] = $value;
}
Any time you want to perform some kind of one-to-one translation, you should immediately think of using an associative array.
Change your query so you are getting the fields AS the field name you want. I don't know what your DB class does, but with mySQL you can do
SELECT `Filename` as `name` FROM table;
Then you can do:
$Info['items'][1] = $results[0];
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.