2013-09-22 54 views
0

我需要循環遍歷行,如下所示,但顯然這個代碼有錯誤。我在這裏添加了foreach循環來顯示我需要循環的地方。 希望是有道理的......數組中間的Foreach循環。或者類似的東西

$data = array(
    'cols' => array(
      array('id' => '', 'label' => 'Date', 'type' => 'string'), 
      array('id' => '', 'label' => 'Score', 'type' => 'number'), 
      array('id' => '', 'label' => 'Result', 'type' => 'number') 
    ), 

    'rows' => array(

     //need this query to loop through the rows 
     $the_query3 = new WP_Query('post_type' => 'handicap'); 
     foreach($the_query3->posts as $post) { 

      array('c' => array(array('v' => get_post_meta($post->ID, 'hcap_date', true)), array('v' => get_post_meta($post->ID, 'hcap_score', true)), array('v' => $result))), 
     };// end foreach loop 
    )   
); 

$chart->load(json_encode($data)); 

回答

1

與空數組初始化元素:

$data = array(
    'cols' => array(
      array('id' => '', 'label' => 'Date', 'type' => 'string'), 
      array('id' => '', 'label' => 'Score', 'type' => 'number'), 
      array('id' => '', 'label' => 'Result', 'type' => 'number') 
    ), 
    'rows' => array()   
); 

然後使用一個循環來填補它在:

foreach($the_query3->posts as $post) { 
    $data['rows'][] = array('c' => array(array('v' => get_post_meta($post->ID, 'hcap_date', true)), array('v' => get_post_meta($post->ID, 'hcap_score', true)), array('v' => $result))); 
} 
+0

曾任職完美!謝謝 – user537137

相關問題