douping3427 2016-10-03 11:30
浏览 68
已采纳

每个循环的PHP - 不生成条目

I am trying to Iterate over an Associative Array which has the following structure

Output from var_export($data)

  1. <?php
  2. Harvest_Project::__set_state(array(
  3. '_root' => 'project',
  4. '_tasks' => array() ,
  5. '_convert' => true,
  6. '_values' => array(
  7. 'id' => '10122036',
  8. 'client-id' => '4861417',
  9. 'name' => 'ABC',
  10. 'code' => '',
  11. 'active' => 'true',
  12. 'billable' => 'true',
  13. 'bill-by' => 'Project',
  14. 'hourly-rate' => '145.0',
  15. 'budget' => '70.0',
  16. 'budget-by' => 'project',
  17. 'notify-when-over-budget' => 'true',
  18. 'over-budget-notification-percentage' => '80.0',
  19. 'over-budget-notified-at' => '2016-09-24',
  20. 'show-budget-to-all' => 'false',
  21. 'created-at' => '2016-03-15T21:38:40Z',
  22. 'updated-at' => '2016-05-31T23:19:58Z',
  23. 'starts-on' => '',
  24. 'ends-on' => '',
  25. 'estimate' => '70.0',
  26. 'estimate-by' => 'project',
  27. 'hint-earliest-record-at' => '2016-03-16',
  28. 'hint-latest-record-at' => '2016-08-11',
  29. 'notes' => '',
  30. 'cost-budget' => '',
  31. 'cost-budget-include-expenses' => 'false',
  32. ) ,
  33. ))

This is the code which I wrote to Iterate over the Array

  1. <?php
  2. $project_id=10122036;
  3. $result=$api->getProject($project_id);
  4. $data = $result->get( "data" );
  5. echo "<table border='1'>
  6. <tr><td>Project Name</td>
  7. <td>Hourly Rate</td>
  8. </tr>";
  9. foreach($data as $key=>$fruit) {
  10. ?>
  11. <tr><td><?php echo $fruit->name;?></td>
  12. <td><?php echo $fruit->{'hourly-rate'};?></td></tr>
  13. <?php }
  14. echo "</table>";
  15. ?>

This code only creates the columns and for some reason does not generate the entries for each row in the table. Hence the resulting table is an Empty table. Kindly suggest where I am going wrong.

Update

This code is for querying Harvest using the Harvest API. This is the PHP Wrapper Library http://mdbitz.com/docs/harvest-api/ which contains relevant classes and methods. getProject($project id) is a method to retrieve project details based on the project ID.

展开全部

  • 写回答

2条回答 默认 最新

  • doumiyi7063 2016-10-03 12:27
    关注

    Added html table code.

    $project_id='10122036';
    $result=$api->getProject($project_id);
    echo "<table border='1'>
        <tr><th>Project Name</th>
        <th>Hourly Rate</th></tr>";
    
    if( $result->isSuccess() ) {
        $project = $result->data;?>
    
    <tr><td><?php echo $project->get( "name" );?></td>
                         <td><?php echo $project->hourly-rate;?></td></tr>
    
    <?php }else{?>
       <tr><td colspan="2">No data from API</td></tr>
    <?php } ?>
    </table>
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)
编辑
预览

报告相同问题?

手机看
程序员都在用的中文IT技术交流社区

程序员都在用的中文IT技术交流社区

专业的中文 IT 技术社区,与千万技术人共成长

专业的中文 IT 技术社区,与千万技术人共成长

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

关注【CSDN】视频号,行业资讯、技术分享精彩不断,直播好礼送不停!

客服 返回
顶部