2016-12-09 6 views
1

私は、この使用の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]; 
    } 
+0

配列が定義され、ここでいくつかのPHPコードを投稿 – Blueblazer172

答えて

0

は私が$the_querywp_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>'; 



     } 
} 
1

そのyoureのあなたのデータはあなたの予想出力と一致doesntのよう達成しようとするものについてのご質問から、かなり混乱を使用して終了しました。しかし、本質的には、サブ配列全体を反復するためには、ループの一部をネストしてforeachする必要があります。

このようなものです。

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

出力:

<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