Php mysql的格式化输出(值数组)
我正在运行一个sql查询以输出字段的值,该字段有一个值数组。如何使用php/css/html格式化这些值Php mysql的格式化输出(值数组),php,html,mysql,Php,Html,Mysql,我正在运行一个sql查询以输出字段的值,该字段有一个值数组。如何使用php/css/html格式化这些值 这是我从以下代码中获得的输出: $rb = $wpdb->get_results("select value from wp_rg_lead_detail where field_number = 33 and lead_
这是我从以下代码中获得的输出:
$rb = $wpdb->get_results("select value
from wp_rg_lead_detail
where field_number = 33
and lead_id =
(select distinct lead_id
from wp_rg_lead_detail
where value = '".$uname."')
");
foreach( $rb as $r){
echo $r->value . "<br/> ";
}
期望输出:
FirstName LastName
testb1 test1
testb2 test2
有人能帮我吗?首先,您需要删除序列化数据中的新行和制表符,然后才能正确取消序列化:
$data = unserialize('a:2:{i:0;a:2:{s:10:"First Name";s:6:"testb1";s:9:"Last Name";s:5:"test1";}i:1;a:2:{s:10:"First Name";s:6:"testb2";s:9:"Last Name";s:5:"test2";}}');
您可以使用以下简单循环输出所需的结果:
// Keep track of whether the headers have been output yet
$headersOutput = false;
foreach ($data as $row) {
// If the headers haven't been output yet
if (!$headersOutput) {
// Output the headers only
echo implode("\t", array_keys($row)), PHP_EOL;
// Update variable so it won't happen again
$headersOutput = true;
}
// Output the values
echo implode("\t", array_values($row)), PHP_EOL;
}
您可以将\t制表符替换为任何要分隔列的字符,并将PHP\u EOL替换为应该分隔新行的字符
如果您想将此数据添加到数组,例如写入CSV,则只需使用$output[]=INPODE。。。而不是echo,并在末尾使用$output。这看起来像是对其进行了序列化。print_r和var_dump是查看数组内部内容的好方法。这是json,而不是mysql输出。如果您只是想拆开json来显示它,那么json编码mysql结果就没有意义了,除非您是在客户端JS代码或其他东西中这样做的。@MarcB这不是json。它看起来像是seralize的输出,而不是json编码。这是我从以下代码得到的输出:$rb=$wpdb->get_resultsselect value from wp_rg_lead_detail,其中字段号=33,lead_id=从wp_rg_lead_detail中选择不同的lead_id,其中值='。$uname';foreach$rb作为$r{echo$r->value.}哎呀,对了。是的,那是连载。不管怎样。。。如果您只需要立即再次将其拉开,那么序列化就没有意义了。
// Keep track of whether the headers have been output yet
$headersOutput = false;
foreach ($data as $row) {
// If the headers haven't been output yet
if (!$headersOutput) {
// Output the headers only
echo implode("\t", array_keys($row)), PHP_EOL;
// Update variable so it won't happen again
$headersOutput = true;
}
// Output the values
echo implode("\t", array_values($row)), PHP_EOL;
}
First Name Last Name
testb1 test1
testb2 test2