- 首页
- PHP
- 在 php 中使用 in_array 时缺少一个值
在 php 中使用 in_array 时缺少一个值
[英] Missing one value while using in_array in php
本文介绍了在 php 中使用 in_array 时缺少一个值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两个数组,下面是输出.第一个数组是我的所有列表,第二个数组是我选择的用户.
$specification=getgeneralSpecifications($pdo);//低于数组大批([0] =>大批([sp_id] =>1[规格名称] =>示例 1)[1] =>大批([sp_id] =>2[规格名称] =>例子2)[2] =>大批([sp_id] =>3[规格名称] =>例3)[3] =>大批([sp_id] =>4[规格名称] =>示例 4)[4] =>大批([sp_id] =>5[规格名称] =>例 5)[5] =>大批([sp_id] =>6[规格名称] =>例 6))$getgeneral = expand(",",$info['developerprofile']['general_Specifications']);//低于输出大批([0] =>1[1] =>2[2] =>3[3] =>4)
我必须显示数据库中的所有列表,并根据获取表单数据库的第二个数组值选中了复选框.
我尝试了下面的代码,我得到了输出但缺少一个值.我的意思是,如果我有数组 1,2,3,4
那么我正在使用 1,2,3
$row) {$checked="";if(in_array($key, $getgeneral)){$checked="已检查";}?><li><label><?php echo $row['specifications_name'];?></label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"value="<?php echo $row['sp_id'];?>";name="general_specification[]";<?php echo $checked;?>>
<?php } ?>
解决方案
这是错误的行索引和 sp_id
值(也是数字)的一个简单问题.
您的 $key
变量将更恰当地命名为 $index
,但事实是您根本不需要声明该变量.相反,在与 $getgeneral
数组进行比较时引用该行的 sp_id
,一切都会好起来的.
我建议创建一个干净的模板字符串以在您迭代时使用.printf()
非常适合这种技术.通过这种方式,您可以整齐地标记您的标记,并且您不需要使用与内联条件块混合的凌乱插值/串联.
哦,我将在 foreach()
中演示数组解构,但您不一定需要这样做 - 如果您愿意,您可以通过它们的键访问子数组值.
代码:(演示)(没有解构的演示)
function getgeneralSpecifications() {返回 [['sp_id' =>1, '规格名称' =>'示例 1'],['sp_id' =>2、'规格名称' =>'示例 2'],['sp_id' =>3、'规格名称' =>'示例 3'],['sp_id' =>4、'规格名称' =>'示例 4'],['sp_id' =>5、'规格名称' =>'示例 5'],['sp_id' =>6、'规格名称' =>'示例 6'],];}$checked = expand(',', '1,2,4');回声<ul>";foreach (getgeneralSpecifications() as ['sp_id' => $id, 'specifications_name' => $name]) {打印输出('<label>%s</label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"值=%d"name="general_specification[]";%s>
',$姓名,$id,in_array($id, $checked) ?'检查':'');}echo "</ul>";
输出:
<li><label>示例 1</label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"值=1"name="general_specification[]";已检查>
<li><label>示例2</label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"值=2"name="general_specification[]";已检查>
<li><label>示例 3</label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"值=3"name="general_specification[]";>
<li><label>示例 4</label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"值=4"name="general_specification[]";已检查>
<li><label>示例 5</label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"值=5"name="general_specification[]";>
<li><label>示例 6</label><div class="form-check"><input class="form-check-input custom-checkbox generalsinglecheck";类型=复选框"值=6"name="general_specification[]";>
I have two arrays and below is the output. The first array is my all the list and the second I am getting by the user selected.
$specification=getgeneralSpecifications($pdo); // getting below array
Array(
[0] => Array
(
[sp_id] => 1
[specifications_name] => example 1
)
[1] => Array
(
[sp_id] => 2
[specifications_name] => example 2
)
[2] => Array
(
[sp_id] => 3
[specifications_name] => example 3
)
[3] => Array
(
[sp_id] => 4
[specifications_name] => example 4
)
[4] => Array
(
[sp_id] => 5
[specifications_name] => example 5
)
[5] => Array
(
[sp_id] => 6
[specifications_name] => example 6
)
)
$getgeneral = explode(",",$info['developerprofile']['general_Specifications']); // getting below output
Array
(
[0] => 1
[1] => 2
[2] => 3
[3] => 4
)
I have to display all the lists from the database and I have checked the check box depending on the second array value getting the form database.
I tried the below code and I am getting the output but missing one value.
I mean if I have array 1,2,3,4
then I am getting on 1,2,3
<?php
$specification=getgeneralSpecifications($pdo);
$getgeneral = explode(",",$info['developerprofile']['general_Specifications']);
foreach ($specification as $key=> $row) {
$checked="";
if(in_array($key, $getgeneral)){
$checked="checked";
}
?>
<li><label><?php echo $row['specifications_name'];?></label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck" type="checkbox" value="<?php echo $row['sp_id'];?>" name="general_specification[]" <?php echo $checked;?>>
</div>
</li>
<?php } ?>
解决方案
This is a simple matter of mistaking the row indexes and the sp_id
values (which are also numeric).
Your $key
variable would be more aptly named $index
, but the truth is that you don't need to declare that variable at all. Instead, reference the row's sp_id
when comparing against the $getgeneral
array and everything will be fine.
I recommend creating a clean template string to use as you iterate. printf()
is great for this technique. This way you can neatly tab your markup and you don't need to use messy interpolation/concatenation mixed with inline condition blocks.
Oh, and I'll demonstrate array destructuring within the foreach()
, but you don't necessarily need to do this -- you can just access the subarray values by their key if you wish.
Code: (Demo) (Demo without destructuring)
function getgeneralSpecifications() {
return [
['sp_id' => 1, 'specifications_name' => 'example 1'],
['sp_id' => 2, 'specifications_name' => 'example 2'],
['sp_id' => 3, 'specifications_name' => 'example 3'],
['sp_id' => 4, 'specifications_name' => 'example 4'],
['sp_id' => 5, 'specifications_name' => 'example 5'],
['sp_id' => 6, 'specifications_name' => 'example 6'],
];
}
$checked = explode(',', '1,2,4');
echo "<ul>";
foreach (getgeneralSpecifications() as ['sp_id' => $id, 'specifications_name' => $name]) {
printf(
'<li>
<label>%s</label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck"
type="checkbox"
value="%d"
name="general_specification[]"
%s>
</div>
</li>',
$name,
$id,
in_array($id, $checked) ? 'checked' : ''
);
}
echo "</ul>";
Output:
<ul>
<li>
<label>example 1</label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck"
type="checkbox"
value="1"
name="general_specification[]"
checked>
</div>
</li>
<li>
<label>example 2</label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck"
type="checkbox"
value="2"
name="general_specification[]"
checked>
</div>
</li>
<li>
<label>example 3</label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck"
type="checkbox"
value="3"
name="general_specification[]"
>
</div>
</li>
<li>
<label>example 4</label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck"
type="checkbox"
value="4"
name="general_specification[]"
checked>
</div>
</li>
<li>
<label>example 5</label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck"
type="checkbox"
value="5"
name="general_specification[]"
>
</div>
</li>
<li>
<label>example 6</label>
<div class="form-check">
<input class="form-check-input custom-checkbox generalsinglecheck"
type="checkbox"
value="6"
name="general_specification[]"
>
</div>
</li>
</ul>
这篇关于在 php 中使用 in_array 时缺少一个值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文