dongren4147 2010-09-14 00:33
浏览 15
已采纳

PHP数组问题?

Hope you are fine. My question :

In MYSQL i have a table with this type of field

Field Name: TAGS Value : xavier,celine,marise,leon,john,cathy,polux,maurice


In PHP i do this

$xwords = array();
function array_rpush(&$arr, $item)
{
  $arr = array_pad($arr, -(count($arr) + 1), $item);
}

$tags = requete("SELECT tags FROM tbl_tags LIMIT 1;");
while($dtags = mysql_fetch_assoc($tags)){
  $words .= array_rpush($xwords, $dtags['tags']);
}

// MY ARRAY XWORDS FOR DEBUG
//
// Array ( [0] => xavier, celine, marise, leon, john, cathy, polux, maurice
//

My script need to find the first letter of each word in this list and check if he match with A / B / C (i create an A-Z index page)

// COUNT $XWORDS VALUE
$total = count($xwords);
// total =1
for($i=0; $i < $total; $i++)
{
 $wtags = explode(",",$xwords[$i]);
 // wtags = Array ( [0] => xavier [1] => celine [2] => marise... )
  while (list($idx,$val) = each($wtags)) {
    echo $val{0}."<br>";
    echo substr($val,0,1)."<br>";
  }
}

echo $val{0}."<br>"; OR echo substr($val,0,1)."<br>" give me just x and nothing after (while give me only the first letter for the first record in array... amazing :))

Perhaps you can help me find a solution. Thanks

  • 写回答

2条回答 默认 最新

  • douxiong2738 2010-09-14 00:52
    关注

    The problem with your code is that it generates:

    Array ( [0] => "xavier" [1] => " celine" [2] => " marise"... )

    So $val[0] = " ". Try to trim($val):

    $val = trim($val);
    print $val[0];
    
    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?
  • ¥20 IOS游览器某宝手机网页版自动立即购买JavaScript脚本
  • ¥15 手机接入宽带网线,如何释放宽带全部速度