我正在尝试将数组中的值打印为字符串。我从PHP文档中获取了此代码,但是我收到了字符串转换错误。值是数字,我可以看到它们
$viz_array_qry = "SELECT (`column_name`) FROM table_name WHERE option_name='$option_name'";
$result = mysql_query($viz_array_qry);
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
$viz_array = implode (',',$result_array);
}
print_r($viz_array);
//echo $viz_array; <!-- no result-->
如果我var_dump $ result_array,我会得到下面的值列表。但我只想将它们显示为
0,0,57,39,40
我无法弄清楚为什么内爆不起作用?
array (size=5)
0 =>
array (size=1)
'column_name' => string '0' (length=1)
1 =>
array (size=1)
'column_name' => string '0' (length=1)
2 =>
array (size=1)
'column_name' => string '57' (length=2)
3 =>
array (size=1)
'column_name' => string '39' (length=2)
4 =>
array (size=1)
'column_name' => string '40' (length=2)
答案 0 :(得分:3)
您应该只存储column_name($row['column_name']
)的值而不是$row
(这是一个数组)并将implode
移到循环之外
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row['column_name'];
}
echo implode(',', $result_array);
答案 1 :(得分:0)
While循环后的内爆:
$viz_array_qry = "SELECT (`column_name`) FROM table_name WHERE option_name='$option_name'";
$result = mysql_query($viz_array_qry);
$result_array = array();
while($row = mysql_fetch_assoc($result))
{
$result_array[] = $row;
}
$viz_array = implode (',',$result_array);
print_r($viz_array);
答案 2 :(得分:0)
即使您不想更改现有代码并将数据存储在数组中,也可以使用array_map()根据需要获取结果
$arr = array(0=>array('column_name'=>'0'),1=>array('column_name'=>'0'),
3=>array('column_name'=>'57'),4=>array('column_name'=>'39'));
function get_col($col) {
return $col["column_name"];
}
$values = implode(array_map("get_col", $arr), ',');
echo $values ;