在表格中显示多维数组数据

时间:2016-12-09 09:51:59

标签: php arrays

我有一个数组,在使用var_dump时输出它:

array(3) { 
    ["taxonomy"]=> string(10) "exam_board"
    ["terms"]=> array(1) { 
        [0]=> string(7) "pearson" 
    } 
    ["field"]=> string(4) "slug" 
} 
pearsonarray(3) { 
    ["taxonomy"]=> string(9) "exam_code" 
    ["terms"]=> array(3) { 
        [0]=> string(4) "a123" 
        [1]=> string(4) "b123" 
        [2]=> string(4) "c123" 
    } 
    ["field"]=> string(4) "slug" 
} 
a123array(3) { 
    ["taxonomy"]=> string(10) "exam_level" 
    ["terms"]=> array(1) { 
        [0]=> string(15) "pearson-a-level" 
    } 
    ["field"]=> string(4) "slug" 
}

这里定义数组:

$tax_query = array();

foreach ( get_object_taxonomies( 'exam' ) as $tax ) {
    if ( isset( $_POST[ $tax ] ) ) {
        $tax_query[] = array(
            'taxonomy' => $tax,
            'terms' => wp_unslash( ( array ) $_POST[ $tax ] ),
            'field' => 'slug',          
        );
    }
}

$args['tax_query'] = $tax_query;
$paged = (get_query_var('paged')) ? get_query_var('paged') : 1;
$args['paged'] = $paged;  
$the_query = new WP_Query( $args );

我想做的是遍历数组并将这些术语输出到表格中。

表格需要看起来像这样

<tr>
    <td>pearson</td>
    <td>a123</td>
    <td>pearson-a-level</td>
</tr>
<tr>
    <td>pearson</td>
    <td>b123</td>
    <td>pearson-a-level</td>
</tr>
<tr>
    <td>pearson</td>
    <td>c123</td>
    <td>pearson-a-level</td>
</tr>    

到目前为止,我的循环返回了所有条款,但我不知道如何单独获取它们,所以我可以在表格中使用它们。

foreach ($tax_query as $key => $value) {           
        var_dump($value);
        echo $value["terms"][0];
    }

2 个答案:

答案 0 :(得分:1)

Its pretty confusing from your question as to what youre trying to achieve as your data doesnt match your expected output. But essentially, you need to nest some foreach loops in order to iterate over the sub-array.

Something like this.

$data = [
    [
        "taxonomy" => "exam_board",
        "terms"    => [
            "pearson",
        ],
        "field"    => "slug",
    ],
    [
        "taxonomy" => "exam_code",
        "terms" => [
            "a123",
            "b123",
            "c123",
        ],
        "field" => "slug",
    ],
    [
        "taxonomy" => "exam_level",
        "terms"    => [
            "pearson-a-level",
        ],
        "field" => "slug",
    ],
];

echo "<table>";
foreach($data as $item) {
    foreach($item['terms'] as $term) {
        echo '<tr>';
        echo '<td>' . $item['taxonomy'] . '</td>';
        echo '<td>' . $term . '</td>';
        echo '</tr>';
    }
}
echo "</table>";

outputs:

<table>
    <tr>
        <td>exam_board</td>
        <td>pearson</td>
    </tr>
    <tr>
        <td>exam_code</td>
        <td>a123</td>
    </tr>
    <tr>
        <td>exam_code</td>
        <td>b123</td>
    </tr>
    <tr>
        <td>exam_code</td>
        <td>c123</td>
    </tr>
    <tr>
        <td>exam_level</td>
        <td>pearson-a-level</td>
    </tr>
</table>

example

答案 1 :(得分:0)

我最终使用Front field$the_query

wp_get_post_terms($post->ID, 'exam_code', array("fields" => "names"));