Php 在if语句中使用ms_sql查询生成的数组的第一个值
好的,基本上我有一个返回数组的查询,然后它被循环:Php 在if语句中使用ms_sql查询生成的数组的第一个值,php,arrays,if-statement,Php,Arrays,If Statement,好的,基本上我有一个返回数组的查询,然后它被循环: $result = mssql_query("SELECT * FROM Segments ORDER BY Squares"); if (!$result) { echo 'query failed'; exit; } while ($row = mssql_fetch_array($result)) { $txtsquares = $row["Squares"
$result = mssql_query("SELECT * FROM Segments ORDER BY Squares");
if (!$result) {
echo 'query failed';
exit;
}
while ($row = mssql_fetch_array($result)) {
$txtsquares = $row["Squares"];
echo $txtsquares;
当回音时,变量$txtsquares等于一个数组值,例如,让我们假设1 2 3 4 5 6 7 8
我需要这个数组/循环。但是我想获取这个数组的第一个值,并在if语句中使用它,如下所示:
value="<?php echo $txtsquares; ?>"
<?php if ($txtsquares == 1) { ?> checked="checked" <?php }
else{ ?> checked="" <?php } ?>/>
value=“”
checked=“checked”checked=”“/>
然而,这显然是错误的,因为该值永远不会等于1,因为它是一个数组。有人能给我指出正确的方向吗?我是PHP新手,所以很抱歉,如果这是一个简单的问题,我已经在谷歌上搜索过了,但运气不好 如果需要使用
$txtsquares
的第一个元素,请尝试以下操作:
value="<?php echo $txtsquares; ?>"
<?php if ($txtsquares[0] == 1) { ?> checked="checked" <?php }
else{ ?> checked="" <?php } ?>/>
value=“”
checked=“checked”checked=”“/>
假设$txtsquares
是一个数字串,而你知道它是一个数字串。。。你可以这样做:
$txtsquares = "1234";
$str_values = str_split($txtsquares);
// array('1', '2', '3', '4')
$int_values = array_map(function($i) { return (int)$i; }, $str_values);
// array(1, 2, 3, 4)
$first_value = $int_values[0];
在
mssql.*
上使用PDO。至少您应该使用sqlsrv.*
。txtsquares
实际上是一个数组,还是一个带有空格分隔值的字符串?