gpt4 book ai didi

php - 如何将数组值与单个变量进行比较?

转载 作者:行者123 更新时间:2023-11-29 18:48:30 25 4
gpt4 key购买 nike

我正在从数据库的单列中获取值,例如 4,5,6,7,8 存储在

$sid=$user['std_list']; 

我想将它与另一个变量 $ctegoryy['id'] 进行比较,该变量具有相同的值,但在不同的行 4,5,6,7,8 中,但在不同的行中。我的比较代码是

<?php   while($ctegoryy=mysql_fetch_array($rsultt)) { ?>

<li style="text-transform: capitalize;">
<?php if($ctegoryy['id']==$user['std_list']){
?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" checked>
<?php }
else {?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" /><?php
} echo $ctegoryy['firstname']."&nbsp&nbsp".$ctegoryy['lastname'];?></li>
<?php }?>

最佳答案

针对您的问题检查此解决方案,将数组值与单个变量进行比较

<?php   
$sid=$user['std_list'];

//from your question its look like you are receiving value in string 4,5,6,7,8. So need to convert in array. If its in array then you can skip following step
$sid = explode(",", $sid);


while($ctegoryy=mysql_fetch_array($rsultt)) { ?>

<li style="text-transform: capitalize;">
<?php if( in_array($ctegoryy['id'] , $sid)){ //PHP in built function to check value exist in array
?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" checked>
<?php }
else {?>
<input name="std_list[]" type="checkbox" value="<?php echo $ctegoryy['id'];?>" /><?php
} echo $ctegoryy['firstname']."&nbsp&nbsp".$ctegoryy['lastname'];?></li>
<?php }?>

关于php - 如何将数组值与单个变量进行比较?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44470404/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com