0

我需要遍历如下所示的行,但显然这段代码出错了。我刚刚在此处添加了 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));
4

1 回答 1

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)));
}
于 2013-09-22T04:52:36.453 回答