dongyou2635 2012-03-19 19:22
浏览 45
已采纳

只打印出某个值PHP末尾有“a”的结果集?

I have a foreach loop that iterates over objects in an array. Currently, it outputs information for every element. I need to print out data for only the objects that have an "a" at the end of their id property. Code is as follows:

foreach ($obj as $key => $value) {
    echo '<tr id="subRow">';
    echo '<td>' . $value->date . '</td>';
    echo '<td class="leftLine">' . $value->Name . '</td>';
    echo '<td class="leftLine">' . $value->dealType . '</td>';
    echo '<td class="leftLine">' . $value->id . '</td>';
    echo '<td class="leftLine">' . 'Adobe PDF' . '</td>';
    echo '</tr>';
}

So for every element of the $obj that contains an ID number with an "a" at the end of it, print out that entire element.

  • 写回答

1条回答 默认 最新

  • donglian7879 2012-03-19 19:24
    关注

    substr can help with that:

    foreach ($obj as $key => $value) {
        if(substr($value->id, -1) === 'a') {
            echo '<tr id="subRow">';
            echo '<td>' . $value->date . '</td>';
            echo '<td class="leftLine">' . $value->Name . '</td>';
            echo '<td class="leftLine">' . $value->dealType . '</td>';
            echo '<td class="leftLine">' . $value->id . '</td>';
            echo '<td class="leftLine">' . 'Adobe PDF' . '</td>';
            echo '</tr>';
        }
    }
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 乌班图ip地址配置及远程SSH
  • ¥15 怎么让点阵屏显示静态爱心,用keiluVision5写出让点阵屏显示静态爱心的代码,越快越好
  • ¥15 PSPICE制作一个加法器
  • ¥15 javaweb项目无法正常跳转
  • ¥15 VMBox虚拟机无法访问
  • ¥15 skd显示找不到头文件
  • ¥15 机器视觉中图片中长度与真实长度的关系
  • ¥15 fastreport table 怎么只让每页的最下面和最顶部有横线
  • ¥15 java 的protected权限 ,问题在注释里
  • ¥15 这个是哪里有问题啊?