我的react-router
列是这样的:
第一行:tags
第二行:sky - earth - sea
第三行:iron - silver - gold
......等等
想要从每个项目创建一个div,如下所示:
apple - fruit - food
<div class='tagdown'>sky</div>
<div class='tagdown'>earth</div>
注意:$st = $db->query("select tags from posts");
$arr = array();
$items = "";
while ($row = $st->fetch()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $item) {
$items .= "<div class='tagdown'>" . $item . "</div>\n";
}
echo $items;
另一个尝试:
Array to string conversion...
注意:for ($i = 0; $i < count($arr); ++$i) {
$items .= "<div class='tagdown'>" . $arr[$i] . "</div>\n";
}
echo $items;
任何帮助?
答案 0 :(得分:1)
不要再推动并再次遍历你的阵列。只需在while循环中打印出数据。请尝试以下代码:
$items = "";
while ($row = $st->fetch()) {
$arr = explode(' - ', $row['tags']);
$items .= "<div class='tagdown'>".implode("</div>\n<div class='tagdown'>",$arr)."</div>\n";
}
echo $items;
答案 1 :(得分:1)
示例:
<?php
$items = "";
$str = "sky-earth-sea";
$arr = explode("-", $str);
$count = count($arr);
for($i = 0; $i < $count; $i++) {
$items .= "<div class='tagdown'>".$arr[$i]."</div></br>";
}
echo $items;
?>
答案 2 :(得分:1)
explode()
返回一个数组,你正在将一个数组推入另一个数组
它制作1 2D阵列你可以使用print_r($arr);
检查thar
使用这个
while ($row = $st->fetch()) {
$tag=explode('-', $row['tags'];
foreach($tag as $t){
array_push($arr,$t ));
}
}
答案 3 :(得分:0)
如果使用mysqli_connect
,您还可以使用fetch associative while ($row = $result->fetch_assoc()) {
array_push($arr, explode(' - ', $row['tags']));
}
foreach($arr as $a) {
foreach($a as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;
--------------或-------------
$arr = array();
$items = "";
while ($row = $result->fetch_assoc()) {
$tag = explode(' - ', $row['tags']);
foreach($tag as $v){
$items .= "<div class='tagdown'>" . $v . "</div>\n";
}
}
echo $items;