用逗号分隔的列
我可以用逗号分隔连接列,但是如果在第二列是空的,如何删除逗号? 在下面,我有2列我想加入otot2和otot3 1.如何在otot2后不显示逗号如果otot3 null? 2.如何otot2和otot 3 null如何不显示任何逗号? 3. otot2和otot3灵活(并不总是有数据)用逗号分隔的列
这里我对输出的查询PHP代码:
if(mysqli_num_rows($hasil) > 0)
{
$response = array();
while($data = mysqli_fetch_array($hasil))
{
$h['main muscle'] = $data['otot1'];
$h['other muscle'] = $data['otot2'].', '.$data['otot3'];
array_push($response, $h);
}
$response["success"];
echo json_encode($response);
一个简单的解决办法是 -
$response = array();
while($data = mysqli_fetch_array($hasil))
{
// other code
$temp = array($data['otot2'], $data['otot3']); // Store them in array
$temp = array_filter($temp); // remove empty values
$h['other muscle'] = implode(',', $temp); // implode them with (,)
// Other codes
array_push($response, $h);
}
通过使用array_filter
& implode
您不必担心,
,因为array_filter
会删除空值,并且如果有第值e数组他们的字符串将会是,
,否则不会。
或者你也可以尝试这个
$h['other muscle'] = $data['otot2'];
if($data['otot3'] !== null)
$h['other muscle'] .= ', ' . $data['otot3']; // Concatenate
感谢您的帮助,即时通讯尝试,如果otot3 null,otot2 stil用逗号显示 –
是的即时尝试你的代码,但逗号仍然显示后otot2当otot3 null,和otot2和otot3 null逗号仍然显示 –
@F_X它应该不。检查演示。你已经实现了哪些代码。 –
如果(ISNULL($数据[ 'otot3']))其他.. – 2016-04-27 06:05:11
@Dagon您能不能给一个完整的代码? –