0

I'm attempting to create an reporting table for woocommerce orders based on meta_fields that change from product to product. These custom order fields are stored as rows in the woocommerce_order_items table as a named key and corresponding value, each in its own row.

This report is meant to show any of the appropriate custom field value by row, with the meta_key as the column header.

I'm able to get the following array from the db, but am struggling with how to format the results into a table, with the meta_key used as a column header, and each meta_value listed in the table appropriately.

Here's a current 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
        )

)

Which I would like to format as:

|  cpf_first_name  |  cpf_last_name  |  cpf_gender  |
|---------------------------------------------------|
|  John            |  Doe            |  Male        |
|  James           |  Doe            |  Male        |
|  Jane            |  Doe            |  Female      |
|__________________|_________________|______________|
  • Hello @bshep does my answer helped you ? – CodeMascot Oct 28 '16 at 11:04
0

Here is the tested solution-

// 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

Here is the screen shot- HTML Table From ARRAY

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.