I have table like this:
我有这样的表:
random_num
----------
0
15
12
...
and I have query SELECT random_num FROM table_name;
.
我查询SELECT random_num FROM table_name;。
I want to fetch array in way like:
我想以这样的方式获取数组:
$row[0] = 0;
$row[1] = 15;
$row[2] = 12;
etc..
But PHP lets me only to fetch it in this way:
但是PHP让我只能以这种方式获取它:
$row[0]['random_num'] = 0;
etc..
How to fetch it as I want?
如何按我的意愿获取它?
2 个解决方案
#1
3
You can use array_map
您可以使用array_map
//PHP >= 5.3
$row = array_map(function($n) {
return $row['random_num'];
}, $row);
//PHP < 5.3
function map($n) {
return $n['random_num'];
}
$row = array_map('map', $row);
//EDIT
$row = array_map('implode', $row) //thanks to hakre
#2
0
I have no idea why you couldn't just use $row[0]['random_num']
, but if you really need to flatten the array:
我不知道为什么你不能只使用$ row [0] ['random_num'],但如果你真的需要展平数组:
foreach( $row as $index => $data ) {
$row[ $index ] = $row[ $index ]['random_num'];
}
#1
3
You can use array_map
您可以使用array_map
//PHP >= 5.3
$row = array_map(function($n) {
return $row['random_num'];
}, $row);
//PHP < 5.3
function map($n) {
return $n['random_num'];
}
$row = array_map('map', $row);
//EDIT
$row = array_map('implode', $row) //thanks to hakre
#2
0
I have no idea why you couldn't just use $row[0]['random_num']
, but if you really need to flatten the array:
我不知道为什么你不能只使用$ row [0] ['random_num'],但如果你真的需要展平数组:
foreach( $row as $index => $data ) {
$row[ $index ] = $row[ $index ]['random_num'];
}