单行爆炸与循环

时间:2013-08-20 01:19:24

标签: php foreach explode

我有一些数据存储在单列mysql DB中,如下所示:

1-Yizle-smallpic-this is pretty cool-2-User-smallpic-testing!1-Yizle-smallpic-this is pretty cool-2-User-smallpic-testing!-

现在我想在for循环中输出该数据,并且需要循环每4个连字符,因为数据输出将是

$uid,$username,$userpicurl,$comment..

到目前为止,我的代码显然有效,但只返回第一次出现的代码。

foreach($result->fetch_assoc() as $v){
    list($uid, $username, $userpicurl, $comment) = explode("-", $v); print "$uid $username $userpicurl $comment";
}

1 个答案:

答案 0 :(得分:0)

foreach($result->fetch_assoc() as $v) {
    $parts = explode("-", $v);
    for ($i = 0; $i < count($parts); $i+=4) {
        list($uid, $username, $userpicurl, $comment) = array_slice($parts, $i, 4);
        print "$uid $username $userpicurl $comment";
    }
}