[英]Check through this array with in_array PHP
I have an $checked array, looks like this: 我有一个$ checked数组,看起来像这样:
array(2) {
[0]=>
array(1) {
["id_kategorii"]=>
string(1) "2"
}
[1]=>
array(1) {
["id_kategorii"]=>
string(1) "4"
}
}
$category array in foreach looks like: foreach中的$ category数组如下所示:
array(2) {
["id"]=>
string(2) "14"
["nazwa"]=>
string(3) "123"
}
array(2) {
["id"]=>
string(1) "8"
["nazwa"]=>
string(5) "ajaja"
}
array(2) {
["id"]=>
string(1) "4"
["nazwa"]=>
string(23) "ale nie no kurde w dupe"
}
etc........ 等等........
But it's not typical array like array('1','2','3'); 但这不是典型的数组,例如array('1','2','3'); So I think because of it this doesn't work but I don't know...
所以我认为因为这个原因不起作用,但我不知道...
And I have PHP code like this: 我有这样的PHP代码:
foreach($categories as $category)
{
$final .= '<tr>';
if( in_array($category['id'], $checked) )
{
$final .= '<td>'.form_checkbox(array('name' => 'categories[]', 'value' => $category['id'], 'checked' => 'checked')).'</td>';
}
else
{
$final .= '<td>'.form_checkbox(array('name' => 'categories[]', 'value' => $category['id'])).'</td>';
}
But always it comes with else, in_array never goes true. 但是in_array总是伴随着其他,所以永远不会成真。
And it should go true for category['id'] when it's 2 and 4. 当类别['id']为2和4时,它应该为true。
Why isn't this in_array working properly? 为什么此in_array无法正常工作?
You need to reform the checked array using array_column()
function. 您需要使用
array_column()
函数array_column()
检查的数组。
$checked = array(array('id_kategorii' => '2'), array('id_kategorii' => '4'));
$new_checked = array_column($checked, 'id_kategorii');
print_r($new_checked);
Then use the $new_checked
array in your in_array()
. 然后在
in_array()
使用$new_checked
数组。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.