这是经过测试的解决方案-
// Your sample array
$sample_array = array(
0 => array(
\'meta_key\' => \'cpf_first_name\',
\'meta_value\' => \'John\'
),
1 => array(
\'meta_key\' => \'cpf_first_name\',
\'meta_value\' => \'James\'
),
2 => array(
\'meta_key\' => \'cpf_first_name\',
\'meta_value\' => \'Jane\'
),
3 => array(
\'meta_key\' => \'cpf_gender\',
\'meta_value\' => \'Male\'
),
4 => array(
\'meta_key\' => \'cpf_gender\',
\'meta_value\' => \'Male\'
),
5 => array(
\'meta_key\' => \'cpf_gender\',
\'meta_value\' => \'Female\'
),
6 => array(
\'meta_key\' => \'cpf_last_name\',
\'meta_value\' => \'Doe\'
),
7 => array(
\'meta_key\' => \'cpf_last_name\',
\'meta_value\' => \'Doe\'
),
8 => array(
\'meta_key\' => \'cpf_last_name\',
\'meta_value\' => \'Doe\'
),
);
// Here starts the real code
$headers = [];
$bodys = [];
foreach ( $sample_array as $array ) {
if ( $array[\'meta_key\'] == \'cpf_first_name\' ){
$bodys[\'cpf_first_name\'][] = $array[\'meta_value\'];
if ( !in_array(\'cpf_first_name\', $headers) )
$headers[] = \'cpf_first_name\';
} elseif ( $array[\'meta_key\'] == \'cpf_last_name\' ) {
$bodys[\'cpf_last_name\'][] = $array[\'meta_value\'];
if ( !in_array(\'cpf_last_name\', $headers) )
$headers[] = \'cpf_last_name\';
} else {
$bodys[\'cpf_gender\'][] = $array[\'meta_value\'];
if ( !in_array(\'cpf_gender\', $headers) )
$headers[] = \'cpf_gender\';
}
}
$body_cpf_gender = $bodys[\'cpf_gender\'];
unset($bodys[\'cpf_gender\']);
$bodys[\'cpf_gender\'] = $body_cpf_gender;
$headers= array_diff($headers, array(\'cpf_gender\'));
array_push($headers, \'cpf_gender\');
echo \'<table>\';
?>
<thead>
<tr>
<?php
foreach ( $headers as $header ) {
echo \'<th>\' . $header . \'</th>\';
}
?>
</tr>
</thead>
<tbody>
<?php for ( $i = 0; $i < count($bodys[\'cpf_gender\']); $i++) { ?>
<tr>
<?php foreach ( $headers as $header) { ?>
<td><?php echo $bodys[$header][$i]; ?></td>
<?php } ?>
</tr>
<?php } ?>
</tbody>
<?php
echo \'</table>\';
// Here ends the real code
这是屏幕截图-