基于动态键值从数组生成HTML表

时间:2016-10-26 作者:bshep

我正试图根据不同产品的meta\\u字段为woocommerce订单创建一个报告表。这些自定义订单字段作为行存储在woocommerce\\u order\\u items表中,作为命名键和相应的值,每个字段位于其自己的行中。

此报告旨在按行显示任何适当的自定义字段值,并以meta\\u键作为列标题。

我可以从db中获得以下数组,但我正在努力将结果格式化到一个表中,将meta\\u键用作列标题,并在表中适当列出每个meta\\u值。

以下是当前的示例阵列:

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
        )

)
我想将其格式化为:

|  cpf_first_name  |  cpf_last_name  |  cpf_gender  |
|---------------------------------------------------|
|  John            |  Doe            |  Male        |
|  James           |  Doe            |  Male        |
|  Jane            |  Doe            |  Female      |
|__________________|_________________|______________|

1 个回复
SO网友:CodeMascot

这是经过测试的解决方案-

// 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
这是屏幕截图-HTML Table From ARRAY

相关推荐

将wp_Query替换为wp_User_Query

我在插件中制作了一些订阅者档案和单曲(个人资料页),其中我还包括了一个“用户单曲”模板,通过template_include. 不过,我正在尝试从插件中删除一些模板,以使其使用主题模板。我用了locate_template( \'single.php\' ) 从活动主题中选择单个模板。我没有使用全局wp_query 在本例中显示我的内容,但页面显示了基于查询默认值的循环(十篇帖子)。我想知道的是,我是否可以完全放弃默认查询,用wp_user_query 我可以将查询到的用户ID输入其中。然后我想筛选the