Foreach循环在数组的中间。或类似的东西

时间:2013-09-22 04:33:20

标签: php arrays 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 个答案:

答案 0 :(得分: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)));
}