嗨,我有一个变量并返回以下内容:
数组(13){ [“meta_id”]=> 字符串(1)“2” [“post_id”]=> 字符串(4)“2112” [“provider”]=> string(12)“Abc 新闻”[“scheme”]=> string(3)“早上” [“观众”] => 字符串(9)“2000” [“min_time”] => 字符串(3)“5” [“max_time”]=> 字符串(3)“100” [“bways”]=> 字符串(2)“10” [“发布日期”]=> 字符串(10) “04-05-2021” [“类型”]=> 字符串(11) “视频”[“方差”]=> 字符串(4) “4k”[“技术”]=> 字符串(9) “JS、HTML5” [“game_id”]=> 字符串(5)“17640”}
这就是得到结果的原因 ---> $meta = $wpdb->get_row( $wpdb->prepare("SELECT * FROM ".$table_name." WHERE post_id = %d", $post_id), 数组_A
目前我输出如下:
$output .="<tr>";
$output .= sprintf('<td>%s</td><td>%s</td>', $attribute, $value);
$output .="</tr>";
上面的代码生成一个 2 列表。我想要一个 4 列的表格,如下图所示。左列最多 5 行,其余行位于另一列。我想要的结果就像下面的截图
有人可以指导我吗?谢谢
基于此数据数组:
$meta = [
"meta_id" => "2",
"post_id" => "2112",
"provider" => "Abc news",
"scheme" => "morning",
"viewers" => "2000",
"min_time" => "5",
"max_time" => "100",
"bways" => "10",
"release_date" => "04-05-2021",
"type" => "Video",
"variance" => "4k",
"tech" => "JS, HTML5",
"game_id" => "17640",
];
您可以使用“模”运算符来获取在 4 列表中分派的数据,例如:
$index = 0;
$count = count($meta);
$output = '<table>
<tr>';
// Loop through array keys / values
foreach ( $meta as $attribute => $value ) {
$output .= sprintf('<td>%s</td><td>%s</td>', $attribute, $value);
$output .= $index % 2 === 0 && ($count - 1) === $index ? '<td></td><td></td>' : '';
$output .= $index % 2 === 1 ? '</tr><tr>' : '';
$index++;
}
$output .= '</tr>
</table>';
它应该按预期工作。