自定义字段数组以每月显示它

时间:2022-01-22 作者:Walled Mahmoud Soliman

我有一个自定义post类型中自定义字段的数组结构,如下所示:


Array
(
    [webw_employee_projects] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_current_projects] => Array
        (
            [January] => 20
            [February] => 50
            [March] => 20
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_min_target] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_max_target] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_target] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_target_bonus] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_max_hours] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_vacation_hours] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_hours] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_hours_bonus] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_karma_points] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_extra_payment] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

    [webw_employee_salary] => Array
        (
            [January] => 0
            [February] => 0
            [March] => 0
            [April] => 0
            [May] => 0
            [June] => 0
            [July] => 0
            [August] => 0
            [September] => 0
            [October] => 0
            [November] => 0
            [December] => 0
        )

)
这里的目标是为每个用户创建表this 每个月都会显示为对他的评估,因此在用户页面中每个月都会有一个表,根据上面的数组结构,如何对其进行排序?

1月[1月数据表]

2月[2月数据表]

3月[3月数据表]

等直到12月。

再次感谢。

1 个回复
SO网友:Adam

这个问题更适合于https://stackoverflow.com 正如Max所建议的,但对于快速而肮脏的解决方案:

解决方案(粗略)

调整代码以符合您的规范,但这应该会为您提供所需的基本结构。

<?php

// assume $meta_data holds your array (e.g. get_post_meta($post_id, \'some_key\', true);
// for some sample data I made an array here: https://pastebin.com/8qgabnAM

$headings = [\'<th class="empty"></th>\'];

$rows = [];

foreach ($meta_data  as $key => $months) {

    $heading = str_replace(\'webw_employee_\', \'\', $key);
    $heading = implode( \' \', explode(\'_\', $heading) );
    $heading = ucwords( $heading );

    $headings[] = "<th>{$heading}</th>";

    foreach ($months  as $month => $value) {
        $rows[$month][$key] = "<td>{$value}</td>";
    }  
}  

foreach ($rows  as $month => $row) {

    $heading = "<th>{$month}</th>";
    $cells   = implode(\'\', $row);

    $rows[$month] = "<tr>{$heading}{$cells}</tr>";

} 

$rows = implode( \'\', $rows );
$headings = implode( \'\', $headings );
$headings = "<tr>{$headings}</tr>";
?>

<table>
    <tbody>
    <?php echo $headings ?>
    <?php echo $rows ?>
    </tbody>
</table>

结果:

注意sample data is random, 显然,数据集的情况会有所不同,这只是为了显示表是如何填充的。

如果需要,您可以轻松地调整它,将每个月都分解到它自己的表中。

enter image description here

结果#2:(使用计算的meta\\u键)

关于注释:

我需要向数据集添加一些值,但无论我做什么,它都会给我0,比方说我想添加工资+额外付款,即使我将其转换为float或int,它也会给我0,你认为问题是什么?ibb。co/C2tt6QV

Please refer to GIST HERE

enter image description here