我如何str_split mysqli数据库的每个字符结果输出我试过:
require("init.php");
$result = mysqli_query($conn, "SELECT item_name , quantity FROM books WHERE book = 1905515");
if($row = mysqli_fetch_assoc($result))
{
$da = $row["item_name"];
$qty = $row["quantity"];
}
$sql = mysqli_query($conn, "SELECT name , recharge , details , logo , price FROM promo WHERE code='$da' LIMIT 1");
if($rrow = mysqli_fetch_assoc($sql))
{
$code = $rrow["recharge"];
$see = str_split($code, 2);
echo "<br/>$see"; // not working fine, outputs 'Array'
echo "$code"; // working fine
}
我得到的结果是&#39; arrary&#39;感谢您的时间和影响
答案 0 :(得分:0)
这里有几个方向,我将举几个例子。
1)如果你只是想要在每个行列中回显一下,那么你只需要这样:
if($rrow = mysqli_fetch_assoc($sql))
{
echo '<br>' . $rrow['recharge'];
}
2)如果你确实需要将'recharge'列分成2个字符的数组值,然后再将它们作为实绳输出,你可以这样做:
if($rrow = mysqli_fetch_assoc($sql))
{
$codebits = str_split($rrow['recharge'],2);
echo '<br>' . implode('',$codebits);
}
3)如果你需要将每个2个字符的分割放在它自己的html行上,你可以将它添加到内爆中,如下所示:
if($rrow = mysqli_fetch_assoc($sql))
{
$codebits = str_split($rrow['recharge'],2);
echo implode('<br>',$codebits);
}
4)但是,你想用“充电”代码做什么或者你想做什么,在这一点上是灵活的。我们可以使用它的各种方式,因为它在数组中,您甚至可以根据需要将每个数组元素包装在更多的html中:
if($rrow = mysqli_fetch_assoc($sql))
{
$codebits = str_split($rrow['recharge'],2);
foreach($codebits as $codebit) {
echo '<div class="something">'. $codebit .'<span>more html</span></div>';
}
}