1

I have an array which outputs this when using 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" 
}

The array is defined here:

$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 );

What I'd like to do is loop through the array and output the terms into a table.

The table will need to look like this

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

The loop I have so far returns all of the terms but I don't know how to get them individually, so I can use them in the table.

foreach ($tax_query as $key => $value) {           
        var_dump($value);
        echo $value["terms"][0];
    }
1
  • post some php code here, where the array is defined Commented Dec 9, 2016 at 9:56

2 Answers 2

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

Sign up to request clarification or add additional context in comments.

Comments

0

I ended up using $the_query and wp_get_post_terms($post->ID, 'exam_code', array("fields" => "names"));

if ( $the_query->have_posts() ) {

        while ( $the_query->have_posts() ) {

            $the_query->the_post();
            // now $query->post is WP_Post Object, use:
            // $query->post->ID, $query->post->post_title, etc.
            $title = $the_query->post->post_title;
            $id = $the_query->post->ID;
            $exam_date = get_post_meta($the_query->post->ID, 'exam_date', true);
            $exam_code = wp_get_post_terms($post->ID, 'exam_code', array("fields" => "names"));
            if($exam_code) { 
                $exam_code = $exam_code[0]; 
            } else { 
                $exam_code = ""; }
            $exam_time = wp_get_post_terms($post->ID, 'exam_time', array("fields" => "names"));
            if($exam_time) { 
                $exam_time = $exam_time[0]; 
            } else { 
                $exam_time = ""; 
            }


            echo '<tr>'; 
            echo '<td class="tg-yw4l">' . $exam_date . '</td>';
            echo '<td class="tg-yw4l">' . $exam_code . '</td>';
            echo '<td class="tg-yw4l">' . $title . '</td>';
            echo '<td class="tg-yw4l">' . $exam_time . '</td>';
            echo '<tr>';



        }
}

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.